This is Unofficial EPICS BASE Doxygen Site
epicsAtomicOSD.cpp
Go to the documentation of this file.
1 
2 /*************************************************************************\
3 * Copyright (c) 2011 LANS LLC, as Operator of
4 * Los Alamos National Laboratory.
5 * EPICS BASE is distributed subject to a Software License Agreement found
6 * in file LICENSE that is included with this distribution.
7 \*************************************************************************/
8 
9 /*
10  * Author: Jeffrey O. Hill
11  * johill@lanl.gov
12  */
13 
14 #include <errno.h>
15 #include <unistd.h>
16 #include <pthread.h>
17 
18 #include "epicsAssert.h"
19 #include "epicsAtomic.h"
20 
21 /*
22  * Slow, but probably correct on all systems.
23  * Useful only if something more efficient isn`t
24  * provided based on knowledge of the compiler
25  * or OS
26  *
27  * A statically initialized pthread mutex doesn`t
28  * need to be destroyed
29  *
30  * !!!!!
31  * !!!!! WARNING
32  * !!!!!
33  * !!!!! Do not use this implementation on systems where
34  * !!!!! code runs at interrupt context. If so, then
35  * !!!!! an implementation must be provided that is based
36  * !!!!! on a compiler intrinsic or an interrupt lock and or
37  * !!!!! a spin lock primitive
38  * !!!!!
39  */
40 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
41 
43 {
44  unsigned countDown = 1000u;
45  int status;
46  while ( true ) {
47  status = pthread_mutex_lock ( & mutex );
48  if ( status == 0 ) return;
49  assert ( status == EINTR );
50  struct timespec retryDelay = { 0, 100000000 };
51  struct timespec remainingDelay;
52  while (nanosleep(&retryDelay, &remainingDelay) == -1 && errno == EINTR)
53  retryDelay = remainingDelay;
54  countDown--;
55  assert ( countDown );
56  }
57 }
58 
60 {
61  const int status = pthread_mutex_unlock ( & mutex );
62  assert ( status == 0 );
63 }
64 
65 
66 // Slow, but probably correct on all systems.
67 // Useful only if something more efficient isn`t
68 // provided based on knowledge of the compiler
69 // or OS
71 {
73  epicsAtomicLock ( & key );
74  epicsAtomicUnlock ( & key );
75 }
#define assert(exp)
Declare that a condition should be true.
Definition: epicsAssert.h:70
pvd::Status status
An EPICS-specific replacement for ANSI C&#39;s assert.
void epicsAtomicMemoryBarrierFallback(void)
void epicsAtomicLock(EpicsAtomicLockKey *)
Defined by POSIX Real Time.
Definition: osdTime.h:21
void epicsAtomicUnlock(EpicsAtomicLockKey *)