Jack Hansdampf / mbed-mqtt-GSOE1

Dependents:   ESP8266MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Threading.h Source File

Threading.h

00001 /**************************************************************************************
00002  * Copyright (c) 2016, Tomoaki Yamaguchi
00003  *
00004  * All rights reserved. This program and the accompanying materials
00005  * are made available under the terms of the Eclipse Public License v1.0
00006  * and Eclipse Distribution License v1.0 which accompany this distribution.
00007  *
00008  * The Eclipse Public License is available at
00009  *    http://www.eclipse.org/legal/epl-v10.html
00010  * and the Eclipse Distribution License is available at
00011  *   http://www.eclipse.org/org/documents/edl-v10.php.
00012  *
00013  * Contributors:
00014  *    Tomoaki Yamaguchi - initial API and implementation and/or initial documentation
00015  **************************************************************************************/
00016 
00017 #ifndef THREADING_H_
00018 #define THREADING_H_
00019 
00020 #include <pthread.h>
00021 #include <semaphore.h>
00022 #include "MQTTSNGWDefines.h"
00023 
00024 namespace MQTTSNGW
00025 {
00026 #define MQTTSNGW_KEY_DIRECTORY "./"
00027 #define MQTTSNGW_RINGBUFFER_KEY   "ringbuffer.key"
00028 #define MQTTSNGW_RB_MUTEX_KEY     "rbmutex.key"
00029 #define MQTTSNGW_RB_SEMAPHOR_NAME "/rbsemaphor"
00030 
00031 /*=====================================
00032          Class Mutex
00033   ====================================*/
00034 class Mutex
00035 {
00036 public:
00037     Mutex();
00038     Mutex(const char* name);
00039     ~Mutex();
00040     void lock(void);
00041     void unlock(void);
00042 
00043 private:
00044     pthread_mutex_t _mutex;
00045     pthread_mutex_t* _pmutex;
00046     int   _shmid;
00047 };
00048 
00049 /*=====================================
00050          Class Semaphore
00051   ====================================*/
00052 class Semaphore
00053 {
00054 public:
00055     Semaphore();
00056     Semaphore(unsigned int val);
00057     Semaphore(const char* name, unsigned int val);
00058     ~Semaphore();
00059     void post(void);
00060     void wait(void);
00061     void timedwait(uint16_t millsec);
00062 
00063 private:
00064     sem_t* _psem;
00065     sem_t  _sem;
00066     char*  _name;
00067 };
00068 
00069 /*=====================================
00070         Class RingBuffer
00071  =====================================*/
00072 class RingBuffer
00073 {
00074 public:
00075     RingBuffer();
00076     RingBuffer(const char* keyDirctory);
00077     ~RingBuffer();
00078     void put(char* buffer);
00079     int get(char* buffer, int bufferLength);
00080     void reset();
00081 private:
00082     void* _shmaddr;
00083     uint16_t* _length;
00084     uint16_t* _start;
00085     uint16_t* _end;
00086     char* _buffer;
00087     int _shmid;
00088     Mutex* _pmx;
00089     bool _createFlg;
00090 };
00091 
00092 
00093 /*=====================================
00094          Class Runnable
00095   ====================================*/
00096 class Runnable
00097 {
00098 public:
00099     Runnable(){}
00100     virtual ~Runnable(){}
00101     virtual void EXECRUN(){}
00102 };
00103 
00104 #define MAGIC_WORD_FOR_THREAD \
00105 public: void EXECRUN() \
00106 { \
00107     try \
00108     { \
00109         run(); \
00110         stopProcess(); \
00111     } \
00112     catch(...) \
00113     { \
00114         throw; \
00115     } \
00116 }
00117 
00118 /*=====================================
00119          Class Thread
00120   ====================================*/
00121 class Thread : virtual public Runnable{
00122 public:
00123     Thread();
00124     ~Thread();
00125     int start(void);
00126     static pthread_t getID();
00127     static bool equals(pthread_t*, pthread_t*);
00128     virtual void initialize(int argc, char** argv);
00129     void stopProcess(void);
00130     void waitStop(void);
00131     void stop(void);
00132 private:
00133     static void* _run(void*);
00134     pthread_t _threadID;
00135 };
00136 
00137 }
00138 
00139 #endif /* THREADING_H_ */