Skip to content

Commit b7fb57d

Browse files
committed
Added Mutex and condvars
1 parent 1ffcf83 commit b7fb57d

File tree

7 files changed

+182
-10
lines changed

7 files changed

+182
-10
lines changed

Makefile

Lines changed: 4 additions & 3 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 Nov 11 18:24:47 2014 Moghrabi Alexandre
8+
## Last update Tue Nov 11 19:56:23 2014 Moghrabi Alexandre
99
##
1010

1111
NAME= libnetwork.a
@@ -17,9 +17,10 @@ SRC= $(SRCDIR)IpAddress.cpp \
1717
$(SRCDIR)Socket.cpp \
1818
$(SRCDIR)TcpSocket.cpp \
1919
$(SRCDIR)WinSocket.cpp \
20-
$(SRCDIR)SocketSelector.cpp \
2120
$(SRCDIR)Selector.cpp \
22-
$(SRCDIR)Thread.cpp
21+
$(SRCDIR)Thread.cpp \
22+
$(SRCDIR)Mutex.cpp \
23+
$(SRCDIR)CondVar.cpp
2324

2425
OBJS= $(SRC:.cpp=.o)
2526

include/CondVar.hh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// CondVar.hh for LibNet in /home/alexmog/projets/LibNet/include
3+
//
4+
// Made by Moghrabi Alexandre
5+
6+
//
7+
// Started on Tue Nov 11 19:43:06 2014 Moghrabi Alexandre
8+
// Last update Tue Nov 11 19:49:16 2014 Moghrabi Alexandre
9+
//
10+
11+
#ifndef CONDVAR_HH_
12+
# define CONDVAR_HH_
13+
14+
# include <pthread.h>
15+
# include "Mutex.hh"
16+
17+
namespace mognetwork
18+
{
19+
class CondVar : public Mutex
20+
{
21+
public:
22+
CondVar();
23+
virtual ~CondVar();
24+
25+
public:
26+
void wait();
27+
void signal();
28+
void broadcast();
29+
void timedwait(const struct timespec* abstime);
30+
31+
protected:
32+
pthread_cond_t m_cond;
33+
};
34+
}; // namespace mognetwork
35+
36+
#endif // !CONDVAR_HH_

include/Mutex.hh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// Mutex.hh for Mutex in /home/alexmog/projets/LibNet/include
3+
//
4+
// Made by Moghrabi Alexandre
5+
6+
//
7+
// Started on Tue Nov 11 19:30:25 2014 Moghrabi Alexandre
8+
// Last update Tue Nov 11 19:47:25 2014 Moghrabi Alexandre
9+
//
10+
11+
#ifndef MUTEX_HH_
12+
# define MUTEX_HH_
13+
14+
# include <pthread.h>
15+
16+
namespace mognetwork
17+
{
18+
class Mutex
19+
{
20+
public:
21+
Mutex();
22+
virtual ~Mutex();
23+
24+
public:
25+
void lock();
26+
void unlock();
27+
void trylock();
28+
29+
protected:
30+
pthread_mutex_t m_mutex;
31+
};
32+
}; // namespace mognetwork
33+
34+
#endif // !MUTEX_HH_

include/Thread.hh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
//
77
// Started on Tue Nov 11 17:31:29 2014 Moghrabi Alexandre
8-
// Last update Tue Nov 11 19:20:30 2014 Moghrabi Alexandre
8+
// Last update Tue Nov 11 19:49:59 2014 Moghrabi Alexandre
99
//
1010

1111
#ifndef THREAD_HH_
@@ -34,6 +34,7 @@ namespace mognetwork
3434
IRunnable& m_runnable;
3535
pthread_t m_thread;
3636
pthread_attr_t m_attr;
37+
bool m_started;
3738
};
3839
}; // namespace mognetwork
3940

src/CondVar.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 Tue Nov 11 19:55:49 2014 Moghrabi Alexandre
9+
//
10+
11+
#include "ThreadException.hh"
12+
#include "CondVar.hh"
13+
14+
namespace mognetwork
15+
{
16+
CondVar::CondVar()
17+
{
18+
if (pthread_cond_init(&m_cond, NULL) != 0)
19+
throw ThreadException("pthread_cond_init error.", __LINE__, __FILE__);
20+
}
21+
22+
CondVar::~CondVar()
23+
{
24+
if (pthread_cond_destroy(&m_cond) != 0)
25+
throw ThreadException("pthread_cond_destroy error.", __LINE__, __FILE__);
26+
}
27+
28+
void CondVar::wait()
29+
{
30+
if (pthread_cond_wait(&m_cond, &m_mutex) != 0)
31+
throw ThreadException("pthread_cond_wait error.", __LINE__, __FILE__);
32+
}
33+
34+
void CondVar::signal()
35+
{
36+
if (pthread_cond_signal(&m_cond) != 0)
37+
throw ThreadException("pthread_cond_signal error.", __LINE__, __FILE__);
38+
}
39+
40+
void CondVar::broadcast()
41+
{
42+
if (pthread_cond_broadcast(&m_cond) != 0)
43+
throw ThreadException("pthread_cond_broadcast error.", __LINE__, __FILE__);
44+
}
45+
46+
void CondVar::timedwait(const struct timespec* abstime)
47+
{
48+
if (pthread_cond_timedwait(&m_cond, &m_mutex, abstime) != 0)
49+
throw ThreadException("pthread_cond_timedwait error.", __LINE__, __FILE__);
50+
}
51+
}; // namespace mognetwork

src/Mutex.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 Tue Nov 11 19:42:12 2014 Moghrabi Alexandre
9+
//
10+
11+
#include "ThreadException.hh"
12+
#include "Mutex.hh"
13+
14+
namespace mognetwork
15+
{
16+
Mutex::Mutex()
17+
{
18+
if (pthread_mutex_init(&m_mutex, NULL) != 0)
19+
throw ThreadException("pthread_mutex_init error", __LINE__, __FILE__);
20+
}
21+
22+
Mutex::~Mutex()
23+
{
24+
if (pthread_mutex_destroy(&m_mutex) != 0)
25+
throw ThreadException("pthread_mutex_destroy error", __LINE__, __FILE__);
26+
}
27+
28+
void Mutex::lock()
29+
{
30+
if (pthread_mutex_lock(&m_mutex) != 0)
31+
throw ThreadException("pthread_mutex_lock error", __LINE__, __FILE__);
32+
}
33+
34+
void Mutex::unlock()
35+
{
36+
if (pthread_mutex_unlock(&m_mutex) != 0)
37+
throw ThreadException("pthread_mutex_unlock error", __LINE__, __FILE__);
38+
}
39+
40+
void Mutex::trylock()
41+
{
42+
if (pthread_mutex_trylock(&m_mutex) != 0)
43+
throw ThreadException("pthread_mutex_trylock error", __LINE__, __FILE__);
44+
}
45+
}; // namepsace mognetwork

src/Thread.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
//
77
// Started on Tue Nov 11 17:37:30 2014 Moghrabi Alexandre
8-
// Last update Tue Nov 11 19:21:19 2014 Moghrabi Alexandre
8+
// Last update Tue Nov 11 19:29:51 2014 Moghrabi Alexandre
99
//
1010

1111
#include <iostream>
@@ -19,9 +19,9 @@ namespace mognetwork
1919
{
2020
if (pthread_attr_init(&m_attr) != 0)
2121
throw ThreadException("pthread_attr_init() error", __LINE__, __FILE__);
22-
if (detach)
23-
if (pthread_attr_setdetachstate(&m_attr, PTHREAD_CREATE_DETACHED) != 0)
24-
throw ThreadException("pthread_attr_setdetachstate error", __LINE__, __FILE__);
22+
if (detach && pthread_attr_setdetachstate(&m_attr, PTHREAD_CREATE_DETACHED) != 0)
23+
throw ThreadException("pthread_attr_setdetachstate error", __LINE__, __FILE__);
24+
m_started = false;
2525
}
2626

2727
Thread::~Thread()
@@ -32,20 +32,24 @@ namespace mognetwork
3232

3333
void Thread::start()
3434
{
35-
if (pthread_create(&m_thread, &m_attr, &Thread::exec, &m_runnable) != 0)
36-
throw ThreadException("Thread creation error", __LINE__, __FILE__);
35+
if (!m_started)
36+
if (pthread_create(&m_thread, &m_attr, &Thread::exec, &m_runnable) != 0)
37+
throw ThreadException("Thread creation error", __LINE__, __FILE__);
38+
m_started = true;
3739
}
3840

3941
void Thread::cancel()
4042
{
4143
if (pthread_cancel(m_thread) != 0)
4244
throw ThreadException("Thread cancel error", __LINE__, __FILE__);
45+
m_started = false;
4346
}
4447

4548
void Thread::join()
4649
{
4750
if (pthread_join(m_thread, NULL) != 0)
4851
throw ThreadException("Thread cannot be joined", __LINE__, __FILE__);
52+
m_started = false;
4953
}
5054

5155
void* Thread::exec(void *thr)

0 commit comments

Comments
 (0)