wip commit. acpi button driver.
[AROS.git] / compiler / pthread / pthread_cond_timedwait.c
blobd01ec656b1531c50e22050f50040be87ada8e220
1 /*
2 Copyright (C) 2014 Szilard Biro
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
21 #include <proto/timer.h>
23 #include "pthread_intern.h"
24 #include "debug.h"
26 int _pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime, BOOL relative)
28 CondWaiter waiter;
29 BYTE signal;
30 ULONG sigs = 0;
31 ULONG timermask = 0;
32 struct MsgPort timermp;
33 struct timerequest timerio;
34 struct Task *task;
36 DB2(bug("%s(%p, %p, %p)\n", __FUNCTION__, cond, mutex, abstime));
38 if (cond == NULL || mutex == NULL)
39 return EINVAL;
41 pthread_testcancel();
43 // initialize static conditions
44 if (SemaphoreIsInvalid(&cond->semaphore))
45 pthread_cond_init(cond, NULL);
47 task = FindTask(NULL);
49 if (abstime)
51 // prepare MsgPort
52 memset( &timermp, 0, sizeof( timermp ) );
53 timermp.mp_Node.ln_Type = NT_MSGPORT;
54 timermp.mp_Flags = PA_SIGNAL;
55 timermp.mp_SigTask = task;
56 signal = AllocSignal(-1);
57 if (signal == -1)
59 signal = SIGB_TIMER_FALLBACK;
60 SetSignal(SIGF_TIMER_FALLBACK, 0);
62 timermp.mp_SigBit = signal;
63 NEWLIST(&timermp.mp_MsgList);
65 // prepare IORequest
66 timerio.tr_node.io_Message.mn_Node.ln_Type = NT_MESSAGE;
67 timerio.tr_node.io_Message.mn_Node.ln_Pri = 0;
68 timerio.tr_node.io_Message.mn_Node.ln_Name = NULL;
69 timerio.tr_node.io_Message.mn_ReplyPort = &timermp;
70 timerio.tr_node.io_Message.mn_Length = sizeof(struct timerequest);
72 // open timer.device
73 if (OpenDevice((STRPTR)TIMERNAME, UNIT_MICROHZ, &timerio.tr_node, 0) != 0)
75 if (timermp.mp_SigBit != SIGB_TIMER_FALLBACK)
76 FreeSignal(timermp.mp_SigBit);
78 return EINVAL;
81 // prepare the device command and send it
82 timerio.tr_node.io_Command = TR_ADDREQUEST;
83 timerio.tr_node.io_Flags = 0;
84 TIMESPEC_TO_TIMEVAL(&timerio.tr_time, abstime);
85 if (!relative)
87 struct timeval starttime;
88 // absolute time has to be converted to relative
89 // GetSysTime can't be used due to the timezone offset in abstime
90 gettimeofday(&starttime, NULL);
91 timersub(&timerio.tr_time, &starttime, &timerio.tr_time);
93 timermask = 1 << timermp.mp_SigBit;
94 sigs |= timermask;
95 SendIO((struct IORequest *)&timerio);
98 // prepare a waiter node
99 waiter.task = task;
100 signal = AllocSignal(-1);
101 if (signal == -1)
103 signal = SIGB_COND_FALLBACK;
104 SetSignal(SIGF_COND_FALLBACK, 0);
106 waiter.sigmask = 1 << signal;
107 sigs |= waiter.sigmask;
109 // add it to the end of the list
110 ObtainSemaphore(&cond->semaphore);
111 AddTail((struct List *)&cond->waiters, (struct Node *)&waiter);
112 ReleaseSemaphore(&cond->semaphore);
114 // wait for the condition to be signalled or the timeout
115 mutex->incond++;
116 pthread_mutex_unlock(mutex);
117 sigs = Wait(sigs);
118 pthread_mutex_lock(mutex);
119 mutex->incond--;
121 // remove the node from the list
122 ObtainSemaphore(&cond->semaphore);
123 Remove((struct Node *)&waiter);
124 ReleaseSemaphore(&cond->semaphore);
126 if (signal != SIGB_COND_FALLBACK)
127 FreeSignal(signal);
129 if (abstime)
131 // clean up the timerequest
132 if (!CheckIO((struct IORequest *)&timerio))
134 AbortIO((struct IORequest *)&timerio);
135 WaitIO((struct IORequest *)&timerio);
137 CloseDevice((struct IORequest *)&timerio);
139 if (timermp.mp_SigBit != SIGB_TIMER_FALLBACK)
140 FreeSignal(timermp.mp_SigBit);
142 // did we timeout?
143 if (sigs & timermask)
144 return ETIMEDOUT;
147 return 0;
150 int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
152 D(bug("%s(%p, %p, %p)\n", __FUNCTION__, cond, mutex, abstime));
154 return _pthread_cond_timedwait(cond, mutex, abstime, FALSE);