3 * @brief Mbedthis Portable Runtime Base Thread Locking Support
9 * Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
11 * This software is distributed under commercial and open source licenses.
12 * You may use the GPL open source license described below or you may acquire
13 * a commercial license from Mbedthis Software. You agree to be fully bound
14 * by the terms of either license. Consult the LICENSE.TXT distributed with
15 * this software for full details.
17 * This software is open source; you can redistribute it and/or modify it
18 * under the terms of the GNU General Public License as published by the
19 * Free Software Foundation; either version 2 of the License, or (at your
20 * option) any later version. See the GNU General Public License for more
21 * details at: http://www.mbedthis.com/downloads/gplLicense.html
23 * This program is distributed WITHOUT ANY WARRANTY; without even the
24 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
26 * This GPL license does NOT permit incorporating this software into
27 * proprietary programs. If you are unable to comply with the GPL, you must
28 * acquire a commercial license to use this software. Commercial licenses
29 * for this software and support services are available from Mbedthis
30 * Software at http://www.mbedthis.com
37 #if BLD_FEATURE_MULTITHREAD
38 /************************************ Code ************************************/
40 void mprInitThreads(MprApp *app)
44 if (app->globalLock == 0) {
45 app->globalLock = mprCreateLock(app);
46 app->allocLock = mprCreateLock(app);
50 /******************************************************************************/
52 void mprTermThreads(MprApp *app)
56 if (app->globalLock) {
57 mprDestroyLock(app->globalLock);
61 MprLock *lock = app->allocLock;
67 /******************************************************************************/
69 MprLock *mprCreateLock(MprCtx ctx)
75 lock = mprAllocType(ctx, MprLock);
78 pthread_mutexattr_t attr;
80 pthread_mutexattr_init(&attr);
81 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
82 pthread_mutex_init(&lock->cs, &attr);
83 pthread_mutexattr_destroy(&attr);
85 InitializeCriticalSectionAndSpinCount(&lock->cs, 5000);
87 lock->cs = semMCreate(SEM_Q_PRIORITY | SEM_DELETE_SAFE |
98 /******************************************************************************/
100 * Destroy a lock. Must be locked on entrance.
103 void mprDestroyLock(MprLock *lock)
111 pthread_mutex_unlock(&lock->cs);
112 pthread_mutex_destroy(&lock->cs);
114 DeleteCriticalSection(&lock->cs);
121 /******************************************************************************/
126 void mprLock(MprLock *lock)
129 * OPT -- Do this just so we can allocate MprApp before we have created its
130 * lock. Should remove this test here and in mprUnlock.
137 pthread_mutex_lock(&lock->cs);
139 EnterCriticalSection(&lock->cs);
141 semTake(lock->cs, WAIT_FOREVER);
145 /******************************************************************************/
147 * Try to attain a lock. Do not block!
150 int mprTryLock(MprLock *lock)
158 if ((err = pthread_mutex_trylock(&lock->cs)) != 0) {
162 return MPR_ERR_CANT_ACCESS;
168 if (TryEnterCriticalSection(&lock->cs) == 0) {
175 rc = semTake(cs, NO_WAIT);
179 if (rc == S_objLib_OBJ_UNAVAILABLE) {
182 return MPR_ERR_CANT_ACCESS;
191 /******************************************************************************/
196 void mprUnlock(MprLock *lock)
203 pthread_mutex_unlock(&lock->cs);
205 LeaveCriticalSection(&lock->cs);
211 /******************************************************************************/
213 * Big global lock. Avoid using this.
216 void mprGlobalLock(MprCtx ctx)
220 app = mprGetApp(ctx);
223 if (app && app->globalLock) {
224 mprLock(app->globalLock);
228 /******************************************************************************/
230 void mprGlobalUnlock(MprCtx ctx)
234 app = mprGetApp(ctx);
237 if (app && app->globalLock) {
238 mprUnlock(app->globalLock);
242 /******************************************************************************/
244 int mprGetCurrentThreadID()
247 return (int) pthread_self();
249 return GetCurrentThreadId();
251 return (int) pthread_self();
255 /******************************************************************************/
256 #endif /* BLD_FEATURE_MULTITHREAD */
264 * vim600: sw=4 ts=4 fdm=marker