tesseract  4.0.0-1-g2a2b
svutil.h
Go to the documentation of this file.
1 // File: svutil.h
3 // Description: ScrollView Utilities
4 // Author: Joern Wanke
5 // Created: Thu Nov 29 2007
6 //
7 // (C) Copyright 2007, Google Inc.
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 //
19 //
20 // SVUtil contains the SVSync, SVSemaphore, SVMutex and SVNetwork
21 // classes, which are used for thread/process creation & synchronization
22 // and network connection.
23 
24 #ifndef TESSERACT_VIEWER_SVUTIL_H_
25 #define TESSERACT_VIEWER_SVUTIL_H_
26 
27 #ifdef _WIN32
28 #include <windows.h>
29 #include "platform.h"
30 #else
31 #include <pthread.h>
32 #include <semaphore.h>
33 #endif
34 
35 #include <string>
36 
37 #ifndef MAX
38 #define MAX(a, b) ((a > b) ? a : b)
39 #endif
40 
41 #ifndef MIN
42 #define MIN(a, b) ((a < b) ? a : b)
43 #endif
44 
46 class SVSync {
47  public:
49  static void StartThread(void *(*func)(void*), void* arg);
51  static void ExitThread();
53  static void StartProcess(const char* executable, const char* args);
54 };
55 
58 class SVSemaphore {
59  public:
61  SVSemaphore();
63  void Signal();
65  void Wait();
66  private:
67 #ifdef _WIN32
68  HANDLE semaphore_;
69 #elif defined(__APPLE__)
70  sem_t *semaphore_;
71 #else
72  sem_t semaphore_;
73 #endif
74 };
75 
78 class SVMutex {
79  public:
81  SVMutex();
83  void Lock();
85  void Unlock();
86  private:
87 #ifdef _WIN32
88  HANDLE mutex_;
89 #else
90  pthread_mutex_t mutex_;
91 #endif
92 };
93 
94 // Auto-unlocking object that locks a mutex on construction and unlocks it
95 // on destruction.
96 class SVAutoLock {
97  public:
98  explicit SVAutoLock(SVMutex* mutex) : mutex_(mutex) { mutex->Lock(); }
99  ~SVAutoLock() { mutex_->Unlock(); }
100 
101  private:
102  SVMutex* mutex_;
103 };
104 
109 class SVNetwork {
110  public:
112  SVNetwork(const char* hostname, int port);
113 
115  ~SVNetwork();
116 
118  void Send(const char* msg);
119 
122  char* Receive();
123 
125  void Close();
126 
128  void Flush();
129 
130  private:
132  SVMutex mutex_send_;
134  int stream_;
136  char* msg_buffer_in_;
137 
139  std::string msg_buffer_out_;
140 
141  bool has_content; // Win32 (strtok)
143  char* buffer_ptr_; // Unix (strtok_r)
144 };
145 
146 #endif // TESSERACT_VIEWER_SVUTIL_H_
void Close()
Close the connection to the server.
Definition: svutil.cpp:275
static void StartProcess(const char *executable, const char *args)
Starts a new process.
Definition: svutil.cpp:120
void Unlock()
Unlocks on a mutex.
Definition: svutil.cpp:78
void Send(const char *msg)
Put a message in the messagebuffer to the server and try to send it.
Definition: svutil.cpp:209
static void StartThread(void *(*func)(void *), void *arg)
Create new thread.
Definition: svutil.cpp:87
~SVNetwork()
Destructor.
Definition: svutil.cpp:455
void Lock()
Locks on a mutex.
Definition: svutil.cpp:70
void Wait()
Wait on a semaphore.
Definition: svutil.cpp:198
void Signal()
Signal a semaphore.
Definition: svutil.cpp:188
~SVAutoLock()
Definition: svutil.h:99
SVAutoLock(SVMutex *mutex)
Definition: svutil.h:98
char * Receive()
Definition: svutil.cpp:227
The SVSync class provides functionality for Thread & Process Creation.
Definition: svutil.h:46
SVSemaphore()
Sets up a semaphore.
Definition: svutil.cpp:172
SVMutex()
Sets up a new mutex.
Definition: svutil.cpp:62
Definition: svutil.h:78
SVNetwork(const char *hostname, int port)
Set up a connection to hostname on port.
Definition: svutil.cpp:392
static void ExitThread()
Signals a thread to exit.
Definition: svutil.cpp:111
void Flush()
Flush the buffer.
Definition: svutil.cpp:216