zenilib: zenilib/jni/external/sdl/src/thread/pthread/SDL_syscond.c Source File

zenilib 0.5.3.0

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SDL_syscond.c
Go to the documentation of this file.
1 /*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "SDL_config.h"
22
23 #include <sys/time.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <pthread.h>
27
28 #include "SDL_thread.h"
29 #include "SDL_sysmutex_c.h"
30
31 struct SDL_cond
32 {
33 pthread_cond_t cond;
34 };
35
36 /* Create a condition variable */
37 SDL_cond *
38 SDL_CreateCond(void)
39 {
40 SDL_cond *cond;
41
42 cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond));
43 if (cond) {
44 if (pthread_cond_init(&cond->cond, NULL) < 0) {
45 SDL_SetError("pthread_cond_init() failed");
46 SDL_free(cond);
47 cond = NULL;
48 }
49 }
50 return (cond);
51 }
52
53 /* Destroy a condition variable */
54 void
55 SDL_DestroyCond(SDL_cond * cond)
56 {
57 if (cond) {
58 pthread_cond_destroy(&cond->cond);
59 SDL_free(cond);
60 }
61 }
62
63 /* Restart one of the threads that are waiting on the condition variable */
64 int
65 SDL_CondSignal(SDL_cond * cond)
66 {
67 int retval;
68
69 if (!cond) {
70 return SDL_SetError("Passed a NULL condition variable");
71 }
72
73 retval = 0;
74 if (pthread_cond_signal(&cond->cond) != 0) {
75 return SDL_SetError("pthread_cond_signal() failed");
76 }
77 return retval;
78 }
79
80 /* Restart all threads that are waiting on the condition variable */
81 int
82 SDL_CondBroadcast(SDL_cond * cond)
83 {
84 int retval;
85
86 if (!cond) {
87 return SDL_SetError("Passed a NULL condition variable");
88 }
89
90 retval = 0;
91 if (pthread_cond_broadcast(&cond->cond) != 0) {
92 return SDL_SetError("pthread_cond_broadcast() failed");
93 }
94 return retval;
95 }
96
97 int
98 SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
99 {
100 int retval;
101 struct timeval delta;
102 struct timespec abstime;
103
104 if (!cond) {
105 return SDL_SetError("Passed a NULL condition variable");
106 }
107
108 gettimeofday(&delta, NULL);
109
110 abstime.tv_sec = delta.tv_sec + (ms / 1000);
111 abstime.tv_nsec = (delta.tv_usec + (ms % 1000) * 1000) * 1000;
112 if (abstime.tv_nsec > 1000000000) {
113 abstime.tv_sec += 1;
114 abstime.tv_nsec -= 1000000000;
115 }
116
117 tryagain:
118 retval = pthread_cond_timedwait(&cond->cond, &mutex->id, &abstime);
119 switch (retval) {
120 case EINTR:
121 goto tryagain;
122 break;
123 case ETIMEDOUT:
124 retval = SDL_MUTEX_TIMEDOUT;
125 break;
126 case 0:
127 break;
128 default:
129 retval = SDL_SetError("pthread_cond_timedwait() failed");
130 }
131 return retval;
132 }
133
134 /* Wait on the condition variable, unlocking the provided mutex.
135 The mutex must be locked before entering this function!
136 */
137 int
138 SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex)
139 {
140 if (!cond) {
141 return SDL_SetError("Passed a NULL condition variable");
142 } else if (pthread_cond_wait(&cond->cond, &mutex->id) != 0) {
143 return SDL_SetError("pthread_cond_wait() failed");
144 }
145 return 0;
146 }
147
148 /* vi: set ts=4 sw=4 expandtab: */
SDL_DestroyCond
DECLSPEC void SDLCALL SDL_DestroyCond(SDL_cond *cond)
Definition: SDL_syscond.c:64
SDL_CondWaitTimeout
DECLSPEC int SDLCALL SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms)
Definition: SDL_syscond.c:160
NULL
#define NULL
Definition: ftobjs.h:61
SDL_free
DECLSPEC void SDLCALL SDL_free(void *mem)
SDL_CondBroadcast
DECLSPEC int SDLCALL SDL_CondBroadcast(SDL_cond *cond)
Definition: SDL_syscond.c:106
SDL_config.h
Uint32
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:145
SDL_CondSignal
DECLSPEC int SDLCALL SDL_CondSignal(SDL_cond *cond)
Definition: SDL_syscond.c:82
SDL_SetError
DECLSPEC int SDLCALL SDL_SetError(const char *fmt,...)
Definition: SDL_error.c:53
SDL_malloc
DECLSPEC void *SDLCALL SDL_malloc(size_t size)
SDL_CreateCond
DECLSPEC SDL_cond *SDLCALL SDL_CreateCond(void)
Definition: SDL_syscond.c:42
SDL_mutex
struct SDL_mutex SDL_mutex
Definition: SDL_mutex.h:59
SDL_sysmutex_c.h
SDL_MUTEX_TIMEDOUT
#define SDL_MUTEX_TIMEDOUT
Definition: SDL_mutex.h:44
SDL_CondWait
DECLSPEC int SDLCALL SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex)
Definition: SDL_syscond.c:215
SDL_cond
struct SDL_cond SDL_cond
Definition: SDL_mutex.h:167