Skip to content

Commit 1833874

Browse files
committed
Added windows implementation of CondVar and Mutex
1 parent 2c815fd commit 1833874

File tree

12 files changed

+143
-12
lines changed

12 files changed

+143
-12
lines changed

CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ set(CMAKE_BUILD_TYPE Release)
77
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
88

99
add_library(mognetwork SHARED
10-
src/CondVar.cpp
10+
src/UnixCondVar.cpp
11+
src/WinCondVar.cpp
1112
src/IpAddress.cpp
12-
src/Mutex.cpp
13+
src/WinMutex.cpp
14+
src/UnixMutex.cpp
1315
src/Packet.cpp
1416
src/Selector.cpp
1517
src/Socket.cpp

Makefile.old

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
##
77
## Started on Fri Jun 6 11:16:50 2014 Moghrabi Alexandre
8-
## Last update Tue Dec 2 16:53:00 2014 Moghrabi Alexandre
8+
## Last update Sat Dec 6 06:28:18 2014 Moghrabi Alexandre
99
##
1010

1111
NAME= mognetwork
@@ -24,7 +24,8 @@ INCDIR= ./include/
2424

2525
SRC= $(SRCDIR)CondVar.cpp \
2626
$(SRCDIR)IpAddress.cpp \
27-
$(SRCDIR)Mutex.cpp \
27+
$(SRCDIR)WinMutex.cpp \
28+
$(SRCDIR)UnixMutex.cpp \
2829
$(SRCDIR)Selector.cpp \
2930
$(SRCDIR)Socket.cpp \
3031
$(SRCDIR)TcpASIODatas.cpp \

include/mognetwork/CondVar.hh

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
//
77
// Started on Tue Nov 11 19:43:06 2014 Moghrabi Alexandre
8-
// Last update Tue Nov 18 13:11:35 2014 Moghrabi Alexandre
8+
// Last update Sat Dec 6 06:47:20 2014 Moghrabi Alexandre
99
//
1010

1111
/*!
@@ -18,7 +18,11 @@
1818
#ifndef MOGNETWORK_CONDVAR_HH
1919
# define MOGNETWORK_CONDVAR_HH
2020

21+
#ifndef OS_WINDOWS
2122
# include <pthread.h>
23+
#else
24+
# include <windows.h>
25+
#endif // !OS_WINDOWS
2226
# include "Mutex.hh"
2327

2428
namespace mognetwork
@@ -27,7 +31,10 @@ namespace mognetwork
2731
* \class CondVar
2832
* \brief Encapsulation des variables conditionnelles
2933
*/
30-
class CondVar : public Mutex
34+
class CondVar
35+
#ifndef OS_WINDOWS
36+
: public Mutex
37+
#endif
3138
{
3239
public:
3340
/*!
@@ -56,7 +63,12 @@ namespace mognetwork
5663
void timedwait(const struct timespec* abstime);
5764

5865
protected:
66+
#ifndef OS_WINDOWS
5967
pthread_cond_t m_cond; /*!< Variable conditionnelle */
68+
#else
69+
CONDITION_VARIABLE m_cond;
70+
PSRWLOCK m_lock;
71+
#endif // !OS_WINDOWS
6072
};
6173
} // namespace mognetwork
6274

include/mognetwork/Mutex.hh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
//
77
// Started on Tue Nov 11 19:30:25 2014 Moghrabi Alexandre
8-
// Last update Tue Nov 18 13:10:38 2014 Moghrabi Alexandre
8+
// Last update Sat Dec 6 06:19:34 2014 Moghrabi Alexandre
99
//
1010

1111
/*!
@@ -18,7 +18,11 @@
1818
#ifndef MOGNETWORK_MUTEX_HH
1919
# define MOGNETWORK_MUTEX_HH
2020

21+
#ifndef OS_WINDOWS
2122
# include <pthread.h>
23+
#else
24+
# include <windows.h>
25+
#endif
2226

2327
namespace mognetwork
2428
{
@@ -51,7 +55,11 @@ namespace mognetwork
5155
int trylock();
5256

5357
protected:
58+
#ifndef OS_WINDOWS
5459
pthread_mutex_t m_mutex; /*!< données de la mutex */
60+
#else
61+
HANDLE m_mutex;
62+
#endif
5563
};
5664
} // namespace mognetwork
5765

src/TcpSocket.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
//
77
// Started on Thu Jun 5 20:09:34 2014 mognetworkhrabi Alexandre
8-
// Last update Tue Dec 2 16:26:14 2014 Moghrabi Alexandre
8+
// Last update Sat Dec 6 05:26:38 2014 Moghrabi Alexandre
99
//
1010

1111
#include <sys/types.h>

src/CondVar.cpp renamed to src/UnixCondVar.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
66
//
77
// Started on Tue Nov 11 19:48:17 2014 Moghrabi Alexandre
8-
// Last update Tue Nov 25 16:50:49 2014 Moghrabi Alexandre
8+
// Last update Sat Dec 6 06:38:54 2014 Moghrabi Alexandre
99
//
1010

1111
#include "mognetwork/ThreadException.hh"
1212
#include "mognetwork/CondVar.hh"
1313

14+
#ifndef OS_WINDOWS
15+
1416
namespace mognetwork
1517
{
1618
CondVar::CondVar()
@@ -49,3 +51,5 @@ namespace mognetwork
4951
throw ThreadException("pthread_cond_timedwait error.", __LINE__, __FILE__);
5052
}
5153
} // namespace mognetwork
54+
55+
#endif // OS_WINDOWS

src/Mutex.cpp renamed to src/UnixMutex.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
66
//
77
// Started on Tue Nov 11 19:36:19 2014 Moghrabi Alexandre
8-
// Last update Tue Nov 25 16:50:37 2014 Moghrabi Alexandre
8+
// Last update Sat Dec 6 06:18:06 2014 Moghrabi Alexandre
99
//
1010

1111
#include "mognetwork/ThreadException.hh"
1212
#include "mognetwork/Mutex.hh"
1313

14+
#ifndef OS_WINDOWS
15+
1416
namespace mognetwork
1517
{
1618
Mutex::Mutex()
@@ -42,3 +44,5 @@ namespace mognetwork
4244
return pthread_mutex_trylock(&m_mutex);
4345
}
4446
} // namepsace mognetwork
47+
48+
#endif // !OS_WINDOWS

src/UnixThread.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
66
//
77
// Started on Tue Nov 11 17:37:30 2014 Moghrabi Alexandre
8-
// Last update Sun Nov 30 16:55:36 2014 Moghrabi Alexandre
8+
// Last update Sat Dec 6 06:17:23 2014 Moghrabi Alexandre
99
//
1010

1111
#include "mognetwork/OS.hh"
1212

13-
#if defined(OS_LINUX)
13+
#ifndef OS_WINDOWS
1414

1515
#include <iostream>
1616
#include "mognetwork/Thread.hh"

src/WinCondVar.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//
2+
// CondVar.cpp for LibNet in /home/alexmog/projets/LibNet/src
3+
//
4+
// Made by Moghrabi Alexandre
5+
6+
//
7+
// Started on Tue Nov 11 19:48:17 2014 Moghrabi Alexandre
8+
// Last update Sat Dec 6 06:53:09 2014 Moghrabi Alexandre
9+
//
10+
11+
#include "mognetwork/ThreadException.hh"
12+
#include "mognetwork/CondVar.hh"
13+
14+
#if defined OS_WINDOWS
15+
16+
namespace mognetwork
17+
{
18+
CondVar::CondVar()
19+
{
20+
InitializeConditionVariable(&m_cond);
21+
InitializeSRWLock(&m_lock);
22+
}
23+
24+
CondVar::~CondVar()
25+
{
26+
WakeAllConditionVariable(&m_cond);
27+
}
28+
29+
void CondVar::wait()
30+
{
31+
if (CONDITION_VARIABLE_LOCKMODE_SHARED(&m_cond, &m_lock, INFINITE, CONDITION_VARIABLE_LOCKMODE_SHARED) == 0)
32+
throw ThreadException("pthread_cond_wait error.", __LINE__, __FILE__);
33+
}
34+
35+
void CondVar::signal()
36+
{
37+
WakeConditionVariable(&m_cond);
38+
}
39+
40+
void CondVar::broadcast()
41+
{
42+
WakeAllConditionVariable(&m_cond);
43+
}
44+
45+
void CondVar::timedwait(const struct timespec* abstime)
46+
{
47+
(void)abstime;
48+
}
49+
} // namespace mognetwork
50+
51+
#endif // OS_WINDOWS

src/WinMutex.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// Mutex.cpp for Mutex in /home/alexmog/projets/LibNet/src
3+
//
4+
// Made by Moghrabi Alexandre
5+
6+
//
7+
// Started on Tue Nov 11 19:36:19 2014 Moghrabi Alexandre
8+
// Last update Sat Dec 6 06:27:07 2014 Moghrabi Alexandre
9+
//
10+
11+
#include "mognetwork/ThreadException.hh"
12+
#include "mognetwork/Mutex.hh"
13+
14+
#if defined(OS_WINDOWS)
15+
16+
namespace mognetwork
17+
{
18+
Mutex::Mutex()
19+
{
20+
if ((m_mutex = CreateMutex(NULL, FALSE, NULL)) == NULL)
21+
throw ThreadException("pthread_mutex_init error", __LINE__, __FILE__);
22+
}
23+
24+
Mutex::~Mutex()
25+
{
26+
CloseHandle(m_mutex);
27+
}
28+
29+
void Mutex::lock()
30+
{
31+
if (WaitForSingleObject(m_mutex, INFINITE) == WAIT_FAILED)
32+
throw ThreadException("pthread_mutex_lock error", __LINE__, __FILE__);
33+
}
34+
35+
void Mutex::unlock()
36+
{
37+
if (!ReleaseMutex(m_mutex))
38+
throw ThreadException("pthread_mutex_unlock error", __LINE__, __FILE__);
39+
}
40+
41+
int Mutex::trylock()
42+
{
43+
if (WaitForSingleObject(m_mutex, 0) == WAIT_FAILED)
44+
throw ThreadException("pthread_mutex_lock error", __LINE__, __FILE__);
45+
return (0);
46+
}
47+
} // namepsace mognetwork
48+
49+
#endif // !OS_WINDOWS

0 commit comments

Comments
 (0)