tesseract  4.1.3
svutil.h
Go to the documentation of this file.
1 // File: svutil.h
3 // Description: ScrollView Utilities
4 // Author: Joern Wanke
5 //
6 // (C) Copyright 2007, Google Inc.
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
18 //
19 // SVUtil contains the SVSync, SVSemaphore, SVMutex and SVNetwork
20 // classes, which are used for thread/process creation & synchronization
21 // and network connection.
22 
23 #ifndef TESSERACT_VIEWER_SVUTIL_H_
24 #define TESSERACT_VIEWER_SVUTIL_H_
25 
26 #ifdef _WIN32
27 # include "host.h" // also includes windows.h
28 #else
29 #include <pthread.h>
30 #include <semaphore.h>
31 #endif
32 
33 #include <string>
34 
36 class SVSync {
37  public:
39  static void StartThread(void *(*func)(void*), void* arg);
41  static void ExitThread();
43  static void StartProcess(const char* executable, const char* args);
44 };
45 
48 class SVSemaphore {
49  public:
51  SVSemaphore();
53  ~SVSemaphore();
55  void Signal();
57  void Wait();
58  private:
59 #ifdef _WIN32
60  HANDLE semaphore_;
61 #elif defined(__APPLE__)
62  sem_t *semaphore_;
63 #else
64  sem_t semaphore_;
65 #endif
66 };
67 
70 class SVMutex {
71  public:
73  SVMutex();
75  ~SVMutex();
77  void Lock();
79  void Unlock();
80  private:
81 #ifdef _WIN32
82  HANDLE mutex_;
83 #else
84  pthread_mutex_t mutex_;
85 #endif
86 };
87 
88 // Auto-unlocking object that locks a mutex on construction and unlocks it
89 // on destruction.
90 class SVAutoLock {
91  public:
92  explicit SVAutoLock(SVMutex* mutex) : mutex_(mutex) { mutex->Lock(); }
93  ~SVAutoLock() { mutex_->Unlock(); }
94 
95  private:
96  SVMutex* mutex_;
97 };
98 
103 class SVNetwork {
104  public:
106  SVNetwork(const char* hostname, int port);
107 
109  ~SVNetwork();
110 
112  void Send(const char* msg);
113 
116  char* Receive();
117 
119  void Close();
120 
122  void Flush();
123 
124  private:
126  SVMutex mutex_send_;
128  int stream_;
130  char* msg_buffer_in_;
131 
133  std::string msg_buffer_out_;
134 
135  bool has_content; // Win32 (strtok)
137  char* buffer_ptr_; // Unix (strtok_r)
138 };
139 
140 #endif // TESSERACT_VIEWER_SVUTIL_H_
Definition: svutil.h:70
void Lock()
Locks on a mutex.
Definition: svutil.cpp:72
void Unlock()
Unlocks on a mutex.
Definition: svutil.cpp:80
~SVAutoLock()
Definition: svutil.h:93
~SVMutex()
Destroys the mutex.
Definition: svutil.cpp:64
void Signal()
Signal a semaphore.
Definition: svutil.cpp:200
SVAutoLock(SVMutex *mutex)
Definition: svutil.h:92
void Flush()
Flush the buffer.
Definition: svutil.cpp:228
char * Receive()
Definition: svutil.cpp:239
static void StartThread(void *(*func)(void *), void *arg)
Create new thread.
Definition: svutil.cpp:89
SVSemaphore()
Sets up a semaphore.
Definition: svutil.cpp:174
static void StartProcess(const char *executable, const char *args)
Starts a new process.
Definition: svutil.cpp:122
void Wait()
Wait on a semaphore.
Definition: svutil.cpp:210
SVMutex()
Sets up a new mutex.
Definition: svutil.cpp:56
static void ExitThread()
Signals a thread to exit.
Definition: svutil.cpp:113
void Close()
Close the connection to the server.
Definition: svutil.cpp:287
~SVSemaphore()
Cleans up the mutex.
Definition: svutil.cpp:190
~SVNetwork()
Destructor.
Definition: svutil.cpp:413
The SVSync class provides functionality for Thread & Process Creation.
Definition: svutil.h:36
SVNetwork(const char *hostname, int port)
Set up a connection to hostname on port.
Definition: svutil.cpp:338
void Send(const char *msg)
Put a message in the messagebuffer to the server and try to send it.
Definition: svutil.cpp:221