]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/thread.c
Merge changes from CUPS 1.5svn-r9198.
[thirdparty/cups.git] / cups / thread.c
1 /*
2 * "$Id$"
3 *
4 * Threading primitives for CUPS.
5 *
6 * Copyright 2009-2010 by Apple Inc.
7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Apple Inc. and are protected by Federal copyright
10 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
11 * which should have been included with this file. If this file is
12 * file is missing or damaged, see the license at "http://www.cups.org/".
13 *
14 * Contents:
15 *
16 * _cupsMutexLock() - Lock a mutex.
17 * _cupsMutexUnlock() - Unlock a mutex.
18 * _cupsThreadCreate() - Create a thread.
19 */
20
21 /*
22 * Include necessary headers...
23 */
24
25 #include "cups-private.h"
26 #include "thread-private.h"
27
28
29 #if defined(HAVE_PTHREAD_H)
30 /*
31 * '_cupsMutexLock()' - Lock a mutex.
32 */
33
34 void
35 _cupsMutexLock(_cups_mutex_t *mutex) /* I - Mutex */
36 {
37 pthread_mutex_lock(mutex);
38 }
39
40
41 /*
42 * '_cupsMutexUnlock()' - Unlock a mutex.
43 */
44
45 void
46 _cupsMutexUnlock(_cups_mutex_t *mutex) /* I - Mutex */
47 {
48 pthread_mutex_unlock(mutex);
49 }
50
51
52 /*
53 * '_cupsThreadCreate()' - Create a thread.
54 */
55
56 int /* O - 0 on failure, 1 on success */
57 _cupsThreadCreate(
58 _cups_thread_func_t func, /* I - Entry point */
59 void *arg) /* I - Entry point context */
60 {
61 pthread_t thread;
62
63 return (pthread_create(&thread, NULL, (void *(*)(void *))func, arg) == 0);
64 }
65
66
67 #elif defined(WIN32)
68 # include <process.h>
69
70
71 /*
72 * '_cupsMutexLock()' - Lock a mutex.
73 */
74
75 void
76 _cupsMutexLock(_cups_mutex_t *mutex) /* I - Mutex */
77 {
78 if (!mutex->m_init)
79 {
80 _cupsGlobalLock();
81
82 if (!mutex->m_init)
83 {
84 InitializeCriticalSection(&mutex->m_criticalSection);
85 mutex->m_init = 1;
86 }
87
88 _cupsGlobalUnlock();
89 }
90
91 EnterCriticalSection(&mutex->m_criticalSection);
92 }
93
94
95 /*
96 * '_cupsMutexUnlock()' - Unlock a mutex.
97 */
98
99 void
100 _cupsMutexUnlock(_cups_mutex_t *mutex) /* I - Mutex */
101 {
102 LeaveCriticalSection(&mutex->m_criticalSection);
103 }
104
105
106 /*
107 * '_cupsThreadCreate()' - Create a thread.
108 */
109
110 int /* O - 0 on failure, 1 on success */
111 _cupsThreadCreate(
112 _cups_thread_func_t func, /* I - Entry point */
113 void *arg) /* I - Entry point context */
114 {
115 return (_beginthreadex(NULL, 0, (LPTHREAD_START_ROUTINE) func, arg, 0, NULL)
116 != 0);
117 }
118
119
120 #else
121 /*
122 * '_cupsMutexLock()' - Lock a mutex.
123 */
124
125 void
126 _cupsMutexLock(_cups_mutex_t *mutex) /* I - Mutex */
127 {
128 }
129
130
131 /*
132 * '_cupsMutexUnlock()' - Unlock a mutex.
133 */
134
135 void
136 _cupsMutexUnlock(_cups_mutex_t *mutex) /* I - Mutex */
137 {
138 }
139 #endif /* HAVE_PTHREAD_H */
140
141
142 /*
143 * End of "$Id$".
144 */