Commit | Line | Data |
---|---|---|
805e021f CE |
1 | /* |
2 | * Copyright 2000, International Business Machines Corporation and others. | |
3 | * All Rights Reserved. | |
4 | * | |
5 | * This software has been released under the terms of the IBM Public | |
6 | * License. For details, see the LICENSE file in the top-level source | |
7 | * directory or online at http://www.openafs.org/dl/license10.html | |
8 | */ | |
9 | ||
10 | #include <afsconfig.h> | |
11 | #include <afs/param.h> | |
12 | ||
13 | #include <roken.h> | |
14 | ||
15 | #if defined(AFS_NT40_ENV) && defined(AFS_PTHREAD_ENV) | |
16 | #define AFS_GRMUTEX_DECLSPEC __declspec(dllexport) | |
17 | #endif | |
18 | #ifdef AFS_PTHREAD_ENV | |
19 | #include <afs/pthread_glock.h> | |
20 | ||
21 | /* | |
22 | * Implement a pthread based recursive global lock for use in porting | |
23 | * old lwp style code to pthreads. | |
24 | */ | |
25 | ||
26 | pthread_recursive_mutex_t grmutex; | |
27 | ||
28 | static int glock_init = 0; | |
29 | static pthread_once_t glock_init_once = PTHREAD_ONCE_INIT; | |
30 | ||
31 | static void | |
32 | glock_init_func(void) | |
33 | { | |
34 | pthread_mutex_init(&grmutex.mut, (const pthread_mutexattr_t *)0); | |
35 | grmutex.times_inside = 0; | |
36 | grmutex.owner = (pthread_t) 0; | |
37 | grmutex.locked = 0; | |
38 | glock_init = 1; | |
39 | } | |
40 | ||
41 | int | |
42 | pthread_recursive_mutex_lock(pthread_recursive_mutex_t * mut) | |
43 | { | |
44 | int rc = 0; | |
45 | ||
46 | if (!glock_init) | |
47 | pthread_once(&glock_init_once, glock_init_func); | |
48 | ||
49 | if (mut->locked) { | |
50 | if (pthread_equal(mut->owner, pthread_self())) { | |
51 | mut->times_inside++; | |
52 | return rc; | |
53 | } | |
54 | } | |
55 | rc = pthread_mutex_lock(&mut->mut); | |
56 | if (rc == 0) { | |
57 | mut->times_inside = 1; | |
58 | mut->owner = pthread_self(); | |
59 | mut->locked = 1; | |
60 | } | |
61 | ||
62 | return rc; | |
63 | } | |
64 | ||
65 | int | |
66 | pthread_recursive_mutex_unlock(pthread_recursive_mutex_t * mut) | |
67 | { | |
68 | int rc = 0; | |
69 | ||
70 | if (!glock_init) | |
71 | pthread_once(&glock_init_once, glock_init_func); | |
72 | ||
73 | if ((mut->locked) && (pthread_equal(mut->owner, pthread_self()))) { | |
74 | mut->times_inside--; | |
75 | if (mut->times_inside == 0) { | |
76 | mut->locked = 0; | |
77 | rc = pthread_mutex_unlock(&mut->mut); | |
78 | } | |
79 | } else { | |
80 | /* | |
81 | * Note that you might want to try to differentiate between | |
82 | * the two possible reasons you're here, but since we don't | |
83 | * hold the mutex, it's useless to try. | |
84 | */ | |
85 | rc = -1; | |
86 | } | |
87 | return rc; | |
88 | } | |
89 | #endif |