ROOTANA
RootLock.cxx
Go to the documentation of this file.
1 /********************************************************************\
2 
3  Name: RootLock.cxx
4  Created by: Konstantin Olchanski
5 
6  Contents: Helper functions for locking ROOT access in multithreaded network servers
7 
8  $Id$
9 
10 \********************************************************************/
11 
12 #include "RootLock.h"
13 
14 #include <TSemaphore.h>
15 #include <TTimer.h>
16 #include <TThread.h>
17 
18 static TSemaphore gRootSema(0); // wait by server, post by timer
19 static TSemaphore gWaitSema(0); // post by server, wait by timer
20 static TSemaphore gDoneSema(0); // post by server, wait by timer
21 
22 bool gDebugLockRoot = false;
23 
24 void LockRoot()
25 {
26  if (gDebugLockRoot)
27  printf("Try Lock ROOT!\n");
28  gWaitSema.Post();
29  gRootSema.Wait();
30  if (gDebugLockRoot)
31  printf("Lock ROOT!\n");
32 }
33 
34 void UnlockRoot()
35 {
36  if (gDebugLockRoot)
37  printf("Unlock ROOT!\n");
38  gDoneSema.Post();
39 }
40 
42 {
43  fLocked = true;
44  LockRoot();
45 }
46 
48 {
49  if (fLocked)
50  Unlock();
51 }
52 
54 {
55  UnlockRoot();
56  fLocked = false;
57 }
58 
59 class ServerTimer : public TTimer
60 {
61 public:
62 
64 
65  static void StartServerTimer(int period_msec = 100)
66  {
67  if (!fgTimer)
68  {
69  fgTimer = new ServerTimer();
70  fgTimer->Start(period_msec,kTRUE);
71  }
72  }
73 
74  Bool_t Notify()
75  {
76  if (gDebugLockRoot)
77  fprintf(stderr, "ServerTimer::Notify!!\n");
78 
79  int notWaiting = gWaitSema.TryWait();
80  if (gDebugLockRoot)
81  printf("NotWaiting %d!\n", notWaiting);
82  if (!notWaiting)
83  {
84  if (gDebugLockRoot)
85  printf("Yeld root sema!\n");
86  gRootSema.Post();
87  TThread::Self()->Sleep(0, 1000000); // sleep in ns
88  gDoneSema.Wait();
89  if (gDebugLockRoot)
90  printf("Recapture root sema!\n");
91  }
92 
93  Reset();
94  return kTRUE;
95  }
96 
97 private:
98  // ServerTimer is a singleton class,
99  // thus ctor and dtor are private
100 
101  ~ServerTimer() // dtor
102  {
103  //TurnOff();
104  }
105 
106  ServerTimer() // ctor
107  {
108  //int period_msec = 100;
109  //Start(period_msec,kTRUE);
110  }
111 };
112 
114 
115 void StartLockRootTimer(int period_msec)
116 {
117  ServerTimer::StartServerTimer(period_msec);
118 }
119 
120 // end file
bool gDebugLockRoot
Definition: RootLock.cxx:22
void LockRoot()
Definition: RootLock.cxx:24
static TSemaphore gRootSema(0)
void UnlockRoot()
Definition: RootLock.cxx:34
static TSemaphore gWaitSema(0)
void StartLockRootTimer(int period_msec)
Definition: RootLock.cxx:115
static TSemaphore gDoneSema(0)
Bool_t Notify()
Definition: RootLock.cxx:74
static ServerTimer * fgTimer
Definition: RootLock.cxx:63
static void StartServerTimer(int period_msec=100)
Definition: RootLock.cxx:65
void Unlock()
Definition: RootLock.cxx:53
bool fLocked
Definition: RootLock.h:19