xref: /relibc/pthreads-emb/pthread_timechange_handler_np.c (revision 062c5bc4dfeed2c1bed58ed4810dd27adb32c68d)
1*062c5bc4SJason Schmidlapp /*
2*062c5bc4SJason Schmidlapp  * pthread_timechange_handler_np.c
3*062c5bc4SJason Schmidlapp  *
4*062c5bc4SJason Schmidlapp  * Description:
5*062c5bc4SJason Schmidlapp  * This translation unit implements miscellaneous thread functions.
6*062c5bc4SJason Schmidlapp  *
7*062c5bc4SJason Schmidlapp  * --------------------------------------------------------------------------
8*062c5bc4SJason Schmidlapp  *
9*062c5bc4SJason Schmidlapp  *      Pthreads-embedded (PTE) - POSIX Threads Library for embedded systems
10*062c5bc4SJason Schmidlapp  *      Copyright(C) 2008 Jason Schmidlapp
11*062c5bc4SJason Schmidlapp  *
12*062c5bc4SJason Schmidlapp  *      Contact Email: jschmidlapp@users.sourceforge.net
13*062c5bc4SJason Schmidlapp  *
14*062c5bc4SJason Schmidlapp  *
15*062c5bc4SJason Schmidlapp  *      Based upon Pthreads-win32 - POSIX Threads Library for Win32
16*062c5bc4SJason Schmidlapp  *      Copyright(C) 1998 John E. Bossom
17*062c5bc4SJason Schmidlapp  *      Copyright(C) 1999,2005 Pthreads-win32 contributors
18*062c5bc4SJason Schmidlapp  *
19*062c5bc4SJason Schmidlapp  *      Contact Email: rpj@callisto.canberra.edu.au
20*062c5bc4SJason Schmidlapp  *
21*062c5bc4SJason Schmidlapp  *      The original list of contributors to the Pthreads-win32 project
22*062c5bc4SJason Schmidlapp  *      is contained in the file CONTRIBUTORS.ptw32 included with the
23*062c5bc4SJason Schmidlapp  *      source code distribution. The list can also be seen at the
24*062c5bc4SJason Schmidlapp  *      following World Wide Web location:
25*062c5bc4SJason Schmidlapp  *      http://sources.redhat.com/pthreads-win32/contributors.html
26*062c5bc4SJason Schmidlapp  *
27*062c5bc4SJason Schmidlapp  *      This library is free software; you can redistribute it and/or
28*062c5bc4SJason Schmidlapp  *      modify it under the terms of the GNU Lesser General Public
29*062c5bc4SJason Schmidlapp  *      License as published by the Free Software Foundation; either
30*062c5bc4SJason Schmidlapp  *      version 2 of the License, or (at your option) any later version.
31*062c5bc4SJason Schmidlapp  *
32*062c5bc4SJason Schmidlapp  *      This library is distributed in the hope that it will be useful,
33*062c5bc4SJason Schmidlapp  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
34*062c5bc4SJason Schmidlapp  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
35*062c5bc4SJason Schmidlapp  *      Lesser General Public License for more details.
36*062c5bc4SJason Schmidlapp  *
37*062c5bc4SJason Schmidlapp  *      You should have received a copy of the GNU Lesser General Public
38*062c5bc4SJason Schmidlapp  *      License along with this library in the file COPYING.LIB;
39*062c5bc4SJason Schmidlapp  *      if not, write to the Free Software Foundation, Inc.,
40*062c5bc4SJason Schmidlapp  *      59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
41*062c5bc4SJason Schmidlapp  */
42*062c5bc4SJason Schmidlapp 
43*062c5bc4SJason Schmidlapp #include "pthread.h"
44*062c5bc4SJason Schmidlapp #include "implement.h"
45*062c5bc4SJason Schmidlapp 
46*062c5bc4SJason Schmidlapp /*
47*062c5bc4SJason Schmidlapp  * Notes on handling system time adjustments (especially negative ones).
48*062c5bc4SJason Schmidlapp  * ---------------------------------------------------------------------
49*062c5bc4SJason Schmidlapp  *
50*062c5bc4SJason Schmidlapp  * This solution was suggested by Alexander Terekhov, but any errors
51*062c5bc4SJason Schmidlapp  * in the implementation are mine - [Ross Johnson]
52*062c5bc4SJason Schmidlapp  *
53*062c5bc4SJason Schmidlapp  * 1) The problem: threads doing a timedwait on a CV may expect to timeout
54*062c5bc4SJason Schmidlapp  *    at a specific absolute time according to a system timer. If the
55*062c5bc4SJason Schmidlapp  *    system clock is adjusted backwards then those threads sleep longer than
56*062c5bc4SJason Schmidlapp  *    expected. Also, pthreads-embedded converts absolute times to intervals in
57*062c5bc4SJason Schmidlapp  *    order to make use of the underlying OS, and so waiting threads may
58*062c5bc4SJason Schmidlapp  *    awake before their proper abstimes.
59*062c5bc4SJason Schmidlapp  *
60*062c5bc4SJason Schmidlapp  * 2) We aren't able to distinquish between threads on timed or untimed waits,
61*062c5bc4SJason Schmidlapp  *    so we wake them all at the time of the adjustment so that they can
62*062c5bc4SJason Schmidlapp  *    re-evaluate their conditions and re-compute their timeouts.
63*062c5bc4SJason Schmidlapp  *
64*062c5bc4SJason Schmidlapp  * 3) We rely on correctly written applications for this to work. Specifically,
65*062c5bc4SJason Schmidlapp  *    they must be able to deal properly with spurious wakeups. That is,
66*062c5bc4SJason Schmidlapp  *    they must re-test their condition upon wakeup and wait again if
67*062c5bc4SJason Schmidlapp  *    the condition is not satisfied.
68*062c5bc4SJason Schmidlapp  */
69*062c5bc4SJason Schmidlapp 
70*062c5bc4SJason Schmidlapp void *
pthread_timechange_handler_np(void * arg)71*062c5bc4SJason Schmidlapp pthread_timechange_handler_np (void *arg)
72*062c5bc4SJason Schmidlapp /*
73*062c5bc4SJason Schmidlapp  * ------------------------------------------------------
74*062c5bc4SJason Schmidlapp  * DOCPUBLIC
75*062c5bc4SJason Schmidlapp  *      Broadcasts all CVs to force re-evaluation and
76*062c5bc4SJason Schmidlapp  *      new timeouts if required.
77*062c5bc4SJason Schmidlapp  *
78*062c5bc4SJason Schmidlapp  * PARAMETERS
79*062c5bc4SJason Schmidlapp  *      NONE
80*062c5bc4SJason Schmidlapp  *
81*062c5bc4SJason Schmidlapp  *
82*062c5bc4SJason Schmidlapp  * DESCRIPTION
83*062c5bc4SJason Schmidlapp  *      Broadcasts all CVs to force re-evaluation and
84*062c5bc4SJason Schmidlapp  *      new timeouts if required.
85*062c5bc4SJason Schmidlapp  *
86*062c5bc4SJason Schmidlapp  *      This routine may be passed directly to pthread_create()
87*062c5bc4SJason Schmidlapp  *      as a new thread in order to run asynchronously.
88*062c5bc4SJason Schmidlapp  *
89*062c5bc4SJason Schmidlapp  *
90*062c5bc4SJason Schmidlapp  * RESULTS
91*062c5bc4SJason Schmidlapp  *              0               successfully broadcast all CVs
92*062c5bc4SJason Schmidlapp  *              EAGAIN          Not all CVs were broadcast
93*062c5bc4SJason Schmidlapp  *
94*062c5bc4SJason Schmidlapp  * ------------------------------------------------------
95*062c5bc4SJason Schmidlapp  */
96*062c5bc4SJason Schmidlapp {
97*062c5bc4SJason Schmidlapp   int result = 0;
98*062c5bc4SJason Schmidlapp   pthread_cond_t cv;
99*062c5bc4SJason Schmidlapp 
100*062c5bc4SJason Schmidlapp 
101*062c5bc4SJason Schmidlapp   pte_osMutexLock (pte_cond_list_lock);
102*062c5bc4SJason Schmidlapp 
103*062c5bc4SJason Schmidlapp   cv = pte_cond_list_head;
104*062c5bc4SJason Schmidlapp 
105*062c5bc4SJason Schmidlapp   while (cv != NULL && 0 == result)
106*062c5bc4SJason Schmidlapp     {
107*062c5bc4SJason Schmidlapp       result = pthread_cond_broadcast (&cv);
108*062c5bc4SJason Schmidlapp       cv = cv->next;
109*062c5bc4SJason Schmidlapp     }
110*062c5bc4SJason Schmidlapp 
111*062c5bc4SJason Schmidlapp   pte_osMutexUnlock(pte_cond_list_lock);
112*062c5bc4SJason Schmidlapp 
113*062c5bc4SJason Schmidlapp   return (void *) (result != 0 ? EAGAIN : 0);
114*062c5bc4SJason Schmidlapp }
115