tesseract  4.1.1
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  void Signal();
55  void Wait();
56  private:
57 #ifdef _WIN32
58  HANDLE semaphore_;
59 #elif defined(__APPLE__)
60  sem_t *semaphore_;
61 #else
62  sem_t semaphore_;
63 #endif
64 };
65 
68 class SVMutex {
69  public:
71  SVMutex();
73  void Lock();
75  void Unlock();
76  private:
77 #ifdef _WIN32
78  HANDLE mutex_;
79 #else
80  pthread_mutex_t mutex_;
81 #endif
82 };
83 
84 // Auto-unlocking object that locks a mutex on construction and unlocks it
85 // on destruction.
86 class SVAutoLock {
87  public:
88  explicit SVAutoLock(SVMutex* mutex) : mutex_(mutex) { mutex->Lock(); }
89  ~SVAutoLock() { mutex_->Unlock(); }
90 
91  private:
92  SVMutex* mutex_;
93 };
94 
99 class SVNetwork {
100  public:
102  SVNetwork(const char* hostname, int port);
103 
105  ~SVNetwork();
106 
108  void Send(const char* msg);
109 
112  char* Receive();
113 
115  void Close();
116 
118  void Flush();
119 
120  private:
122  SVMutex mutex_send_;
124  int stream_;
126  char* msg_buffer_in_;
127 
129  std::string msg_buffer_out_;
130 
131  bool has_content; // Win32 (strtok)
133  char* buffer_ptr_; // Unix (strtok_r)
134 };
135 
136 #endif // TESSERACT_VIEWER_SVUTIL_H_
Definition: svutil.h:68
void Lock()
Locks on a mutex.
Definition: svutil.cpp:64
void Unlock()
Unlocks on a mutex.
Definition: svutil.cpp:72
~SVAutoLock()
Definition: svutil.h:89
void Signal()
Signal a semaphore.
Definition: svutil.cpp:182
SVAutoLock(SVMutex *mutex)
Definition: svutil.h:88
void Flush()
Flush the buffer.
Definition: svutil.cpp:210
char * Receive()
Definition: svutil.cpp:221
static void StartThread(void *(*func)(void *), void *arg)
Create new thread.
Definition: svutil.cpp:81
SVSemaphore()
Sets up a semaphore.
Definition: svutil.cpp:166
static void StartProcess(const char *executable, const char *args)
Starts a new process.
Definition: svutil.cpp:114
void Wait()
Wait on a semaphore.
Definition: svutil.cpp:192
SVMutex()
Sets up a new mutex.
Definition: svutil.cpp:56
static void ExitThread()
Signals a thread to exit.
Definition: svutil.cpp:105
void Close()
Close the connection to the server.
Definition: svutil.cpp:269
~SVNetwork()
Destructor.
Definition: svutil.cpp:395
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:320
void Send(const char *msg)
Put a message in the messagebuffer to the server and try to send it.
Definition: svutil.cpp:203