Lines Matching refs:lock

23 #include "glthread/lock.h"
38 glthread_rwlock_init_multithreaded (gl_rwlock_t *lock)
42 err = pthread_rwlock_init (&lock->rwlock, NULL);
45 lock->initialized = 1;
50 glthread_rwlock_rdlock_multithreaded (gl_rwlock_t *lock)
52 if (!lock->initialized)
56 err = pthread_mutex_lock (&lock->guard);
59 if (!lock->initialized)
61 err = glthread_rwlock_init_multithreaded (lock);
64 pthread_mutex_unlock (&lock->guard);
68 err = pthread_mutex_unlock (&lock->guard);
72 return pthread_rwlock_rdlock (&lock->rwlock);
76 glthread_rwlock_wrlock_multithreaded (gl_rwlock_t *lock)
78 if (!lock->initialized)
82 err = pthread_mutex_lock (&lock->guard);
85 if (!lock->initialized)
87 err = glthread_rwlock_init_multithreaded (lock);
90 pthread_mutex_unlock (&lock->guard);
94 err = pthread_mutex_unlock (&lock->guard);
98 return pthread_rwlock_wrlock (&lock->rwlock);
102 glthread_rwlock_unlock_multithreaded (gl_rwlock_t *lock)
104 if (!lock->initialized)
106 return pthread_rwlock_unlock (&lock->rwlock);
110 glthread_rwlock_destroy_multithreaded (gl_rwlock_t *lock)
114 if (!lock->initialized)
116 err = pthread_rwlock_destroy (&lock->rwlock);
119 lock->initialized = 0;
128 glthread_rwlock_init_multithreaded (gl_rwlock_t *lock)
132 err = pthread_mutex_init (&lock->lock, NULL);
135 err = pthread_cond_init (&lock->waiting_readers, NULL);
138 err = pthread_cond_init (&lock->waiting_writers, NULL);
141 lock->waiting_writers_count = 0;
142 lock->runcount = 0;
147 glthread_rwlock_rdlock_multithreaded (gl_rwlock_t *lock)
151 err = pthread_mutex_lock (&lock->lock);
157 acquires the lock when a writer does not hold the lock and there are
158 writers blocked on the lock." Let's say, no: give the writers a higher
160 while (!(lock->runcount + 1 > 0 && lock->waiting_writers_count == 0))
164 err = pthread_cond_wait (&lock->waiting_readers, &lock->lock);
167 pthread_mutex_unlock (&lock->lock);
171 lock->runcount++;
172 return pthread_mutex_unlock (&lock->lock);
176 glthread_rwlock_wrlock_multithreaded (gl_rwlock_t *lock)
180 err = pthread_mutex_lock (&lock->lock);
184 while (!(lock->runcount == 0))
188 lock->waiting_writers_count++;
189 err = pthread_cond_wait (&lock->waiting_writers, &lock->lock);
192 lock->waiting_writers_count--;
193 pthread_mutex_unlock (&lock->lock);
196 lock->waiting_writers_count--;
198 lock->runcount--; /* runcount becomes -1 */
199 return pthread_mutex_unlock (&lock->lock);
203 glthread_rwlock_unlock_multithreaded (gl_rwlock_t *lock)
207 err = pthread_mutex_lock (&lock->lock);
210 if (lock->runcount < 0)
212 /* Drop a writer lock. */
213 if (!(lock->runcount == -1))
215 pthread_mutex_unlock (&lock->lock);
218 lock->runcount = 0;
222 /* Drop a reader lock. */
223 if (!(lock->runcount > 0))
225 pthread_mutex_unlock (&lock->lock);
228 lock->runcount--;
230 if (lock->runcount == 0)
234 if (lock->waiting_writers_count > 0)
237 err = pthread_cond_signal (&lock->waiting_writers);
240 pthread_mutex_unlock (&lock->lock);
247 err = pthread_cond_broadcast (&lock->waiting_readers);
250 pthread_mutex_unlock (&lock->lock);
255 return pthread_mutex_unlock (&lock->lock);
259 glthread_rwlock_destroy_multithreaded (gl_rwlock_t *lock)
263 err = pthread_mutex_destroy (&lock->lock);
266 err = pthread_cond_destroy (&lock->waiting_readers);
269 err = pthread_cond_destroy (&lock->waiting_writers);
284 glthread_recursive_lock_init_multithreaded (gl_recursive_lock_t *lock)
298 err = pthread_mutex_init (lock, &attributes);
313 glthread_recursive_lock_init_multithreaded (gl_recursive_lock_t *lock)
327 err = pthread_mutex_init (&lock->recmutex, &attributes);
336 lock->initialized = 1;
341 glthread_recursive_lock_lock_multithreaded (gl_recursive_lock_t *lock)
343 if (!lock->initialized)
347 err = pthread_mutex_lock (&lock->guard);
350 if (!lock->initialized)
352 err = glthread_recursive_lock_init_multithreaded (lock);
355 pthread_mutex_unlock (&lock->guard);
359 err = pthread_mutex_unlock (&lock->guard);
363 return pthread_mutex_lock (&lock->recmutex);
367 glthread_recursive_lock_unlock_multithreaded (gl_recursive_lock_t *lock)
369 if (!lock->initialized)
371 return pthread_mutex_unlock (&lock->recmutex);
375 glthread_recursive_lock_destroy_multithreaded (gl_recursive_lock_t *lock)
379 if (!lock->initialized)
381 err = pthread_mutex_destroy (&lock->recmutex);
384 lock->initialized = 0;
393 glthread_recursive_lock_init_multithreaded (gl_recursive_lock_t *lock)
397 err = pthread_mutex_init (&lock->mutex, NULL);
400 lock->owner = (pthread_t) 0;
401 lock->depth = 0;
406 glthread_recursive_lock_lock_multithreaded (gl_recursive_lock_t *lock)
409 if (lock->owner != self)
413 err = pthread_mutex_lock (&lock->mutex);
416 lock->owner = self;
418 if (++(lock->depth) == 0) /* wraparound? */
420 lock->depth--;
427 glthread_recursive_lock_unlock_multithreaded (gl_recursive_lock_t *lock)
429 if (lock->owner != pthread_self ())
431 if (lock->depth == 0)
433 if (--(lock->depth) == 0)
435 lock->owner = (pthread_t) 0;
436 return pthread_mutex_unlock (&lock->mutex);
443 glthread_recursive_lock_destroy_multithreaded (gl_recursive_lock_t *lock)
445 if (lock->owner != (pthread_t) 0)
447 return pthread_mutex_destroy (&lock->mutex);
532 glthread_recursive_lock_init_multithreaded (gl_recursive_lock_t *lock)
536 err = mutex_init (&lock->mutex, USYNC_THREAD, NULL);
539 lock->owner = (thread_t) 0;
540 lock->depth = 0;
545 glthread_recursive_lock_lock_multithreaded (gl_recursive_lock_t *lock)
548 if (lock->owner != self)
552 err = mutex_lock (&lock->mutex);
555 lock->owner = self;
557 if (++(lock->depth) == 0) /* wraparound? */
559 lock->depth--;
566 glthread_recursive_lock_unlock_multithreaded (gl_recursive_lock_t *lock)
568 if (lock->owner != thr_self ())
570 if (lock->depth == 0)
572 if (--(lock->depth) == 0)
574 lock->owner = (thread_t) 0;
575 return mutex_unlock (&lock->mutex);
582 glthread_recursive_lock_destroy_multithreaded (gl_recursive_lock_t *lock)
584 if (lock->owner != (thread_t) 0)
586 return mutex_destroy (&lock->mutex);
637 glthread_lock_init_func (gl_lock_t *lock)
639 InitializeCriticalSection (&lock->lock);
640 lock->guard.done = 1;
644 glthread_lock_lock_func (gl_lock_t *lock)
646 if (!lock->guard.done)
648 if (InterlockedIncrement (&lock->guard.started) == 0)
649 /* This thread is the first one to need this lock. Initialize it. */
650 glthread_lock_init (lock);
653 initializing this lock. */
654 while (!lock->guard.done)
657 EnterCriticalSection (&lock->lock);
662 glthread_lock_unlock_func (gl_lock_t *lock)
664 if (!lock->guard.done)
666 LeaveCriticalSection (&lock->lock);
671 glthread_lock_destroy_func (gl_lock_t *lock)
673 if (!lock->guard.done)
675 DeleteCriticalSection (&lock->lock);
676 lock->guard.done = 0;
774 glthread_rwlock_init_func (gl_rwlock_t *lock)
776 InitializeCriticalSection (&lock->lock);
777 gl_waitqueue_init (&lock->waiting_readers);
778 gl_waitqueue_init (&lock->waiting_writers);
779 lock->runcount = 0;
780 lock->guard.done = 1;
784 glthread_rwlock_rdlock_func (gl_rwlock_t *lock)
786 if (!lock->guard.done)
788 if (InterlockedIncrement (&lock->guard.started) == 0)
789 /* This thread is the first one to need this lock. Initialize it. */
790 glthread_rwlock_init (lock);
793 initializing this lock. */
794 while (!lock->guard.done)
797 EnterCriticalSection (&lock->lock);
800 if (!(lock->runcount + 1 > 0))
804 HANDLE event = gl_waitqueue_add (&lock->waiting_readers);
808 LeaveCriticalSection (&lock->lock);
815 removed us from the waiting_readers, incremented lock->runcount. */
816 if (!(lock->runcount > 0))
825 LeaveCriticalSection (&lock->lock);
827 EnterCriticalSection (&lock->lock);
829 while (!(lock->runcount + 1 > 0));
832 lock->runcount++;
833 LeaveCriticalSection (&lock->lock);
838 glthread_rwlock_wrlock_func (gl_rwlock_t *lock)
840 if (!lock->guard.done)
842 if (InterlockedIncrement (&lock->guard.started) == 0)
843 /* This thread is the first one to need this lock. Initialize it. */
844 glthread_rwlock_init (lock);
847 initializing this lock. */
848 while (!lock->guard.done)
851 EnterCriticalSection (&lock->lock);
853 if (!(lock->runcount == 0))
857 HANDLE event = gl_waitqueue_add (&lock->waiting_writers);
861 LeaveCriticalSection (&lock->lock);
868 removed us from the waiting_writers, set lock->runcount = -1. */
869 if (!(lock->runcount == -1))
878 LeaveCriticalSection (&lock->lock);
880 EnterCriticalSection (&lock->lock);
882 while (!(lock->runcount == 0));
885 lock->runcount--; /* runcount becomes -1 */
886 LeaveCriticalSection (&lock->lock);
891 glthread_rwlock_unlock_func (gl_rwlock_t *lock)
893 if (!lock->guard.done)
895 EnterCriticalSection (&lock->lock);
896 if (lock->runcount < 0)
898 /* Drop a writer lock. */
899 if (!(lock->runcount == -1))
901 lock->runcount = 0;
905 /* Drop a reader lock. */
906 if (!(lock->runcount > 0))
908 LeaveCriticalSection (&lock->lock);
911 lock->runcount--;
913 if (lock->runcount == 0)
917 if (lock->waiting_writers.count > 0)
920 lock->runcount--;
921 gl_waitqueue_notify_first (&lock->waiting_writers);
926 lock->runcount += lock->waiting_readers.count;
927 gl_waitqueue_notify_all (&lock->waiting_readers);
930 LeaveCriticalSection (&lock->lock);
935 glthread_rwlock_destroy_func (gl_rwlock_t *lock)
937 if (!lock->guard.done)
939 if (lock->runcount != 0)
941 DeleteCriticalSection (&lock->lock);
942 if (lock->waiting_readers.array != NULL)
943 free (lock->waiting_readers.array);
944 if (lock->waiting_writers.array != NULL)
945 free (lock->waiting_writers.array);
946 lock->guard.done = 0;
953 glthread_recursive_lock_init_func (gl_recursive_lock_t *lock)
955 lock->owner = 0;
956 lock->depth = 0;
957 InitializeCriticalSection (&lock->lock);
958 lock->guard.done = 1;
962 glthread_recursive_lock_lock_func (gl_recursive_lock_t *lock)
964 if (!lock->guard.done)
966 if (InterlockedIncrement (&lock->guard.started) == 0)
967 /* This thread is the first one to need this lock. Initialize it. */
968 glthread_recursive_lock_init (lock);
971 initializing this lock. */
972 while (!lock->guard.done)
977 if (lock->owner != self)
979 EnterCriticalSection (&lock->lock);
980 lock->owner = self;
982 if (++(lock->depth) == 0) /* wraparound? */
984 lock->depth--;
992 glthread_recursive_lock_unlock_func (gl_recursive_lock_t *lock)
994 if (lock->owner != GetCurrentThreadId ())
996 if (lock->depth == 0)
998 if (--(lock->depth) == 0)
1000 lock->owner = 0;
1001 LeaveCriticalSection (&lock->lock);
1007 glthread_recursive_lock_destroy_func (gl_recursive_lock_t *lock)
1009 if (lock->owner != 0)
1011 DeleteCriticalSection (&lock->lock);
1012 lock->guard.done = 0;
1026 InitializeCriticalSection (&once_control->lock);
1027 EnterCriticalSection (&once_control->lock);
1031 LeaveCriticalSection (&once_control->lock);
1039 initializing and taking the lock. */
1044 /* Take the lock. This blocks until the other thread has
1046 EnterCriticalSection (&once_control->lock);
1047 LeaveCriticalSection (&once_control->lock);