Skip to content

Commit 04e9a9d

Browse files
sapiersapier
sapier
authored and
sapier
committedDec 1, 2013
Cleanup jthread and fix win32 build
1 parent f3439c4 commit 04e9a9d

29 files changed

+185
-194
lines changed
 

‎src/ban.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ BanManager::BanManager(const std::string &banfilepath):
3131
m_banfilepath(banfilepath),
3232
m_modified(false)
3333
{
34-
m_mutex.Init();
3534
try{
3635
load();
3736
}

‎src/client.cpp

+6-7
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ QueuedMeshUpdate::~QueuedMeshUpdate()
8282

8383
MeshUpdateQueue::MeshUpdateQueue()
8484
{
85-
m_mutex.Init();
8685
}
8786

8887
MeshUpdateQueue::~MeshUpdateQueue()
@@ -474,7 +473,7 @@ void Client::step(float dtime)
474473

475474
core::list<v3s16> deleted_blocks;
476475

477-
float delete_unused_sectors_timeout =
476+
float delete_unused_sectors_timeout =
478477
g_settings->getFloat("client_delete_unused_sectors_timeout");
479478

480479
// Delete sector blocks
@@ -1266,7 +1265,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
12661265
u32 index = 8 + MapNode::serializedLength(ser_version);
12671266
if ((datasize >= index+1) && data[index]){
12681267
remove_metadata = false;
1269-
}
1268+
}
12701269

12711270
addNode(p, n, remove_metadata);
12721271
}
@@ -2126,7 +2125,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
21262125
m_client_event_queue.push_back(event);
21272126
}
21282127
else if(command == TOCLIENT_HUDCHANGE)
2129-
{
2128+
{
21302129
std::string sdata;
21312130
v2f v2fdata;
21322131
u32 intdata = 0;
@@ -2155,7 +2154,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
21552154
m_client_event_queue.push_back(event);
21562155
}
21572156
else if(command == TOCLIENT_HUD_SET_FLAGS)
2158-
{
2157+
{
21592158
std::string datastring((char *)&data[2], datasize - 2);
21602159
std::istringstream is(datastring, std::ios_base::binary);
21612160

@@ -2264,7 +2263,7 @@ void Client::sendNodemetaFields(v3s16 p, const std::string &formname,
22642263
Send(0, data, true);
22652264
}
22662265

2267-
void Client::sendInventoryFields(const std::string &formname,
2266+
void Client::sendInventoryFields(const std::string &formname,
22682267
const std::map<std::string, std::string> &fields)
22692268
{
22702269
std::ostringstream os(std::ios_base::binary);
@@ -2468,7 +2467,7 @@ void Client::sendPlayerPos()
24682467
writeV3S32(&data[2], position);
24692468
writeV3S32(&data[2+12], speed);
24702469
writeS32(&data[2+12+12], pitch);
2471-
writeS32(&data[2+12+12+4], yaw);
2470+
writeS32(&data[2+12+12+4], yaw);
24722471
writeU32(&data[2+12+12+4+4], keyPressed);
24732472
// Send as unreliable
24742473
Send(0, data, false);

‎src/clientmap.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ ClientMap::ClientMap(
5050
m_camera_direction(0,0,1),
5151
m_camera_fov(M_PI)
5252
{
53-
m_camera_mutex.Init();
54-
assert(m_camera_mutex.IsInitialized());
55-
5653
m_box = core::aabbox3d<f32>(-BS*1000000,-BS*1000000,-BS*1000000,
5754
BS*1000000,BS*1000000,BS*1000000);
5855
}

‎src/debug.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ JMutex g_debug_stacks_mutex;
206206

207207
void debug_stacks_init()
208208
{
209-
g_debug_stacks_mutex.Init();
210209
}
211210

212211
void debug_stacks_print_to(std::ostream &os)

‎src/emerge.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2323
#include "server.h"
2424
#include <iostream>
2525
#include <queue>
26+
#include "jthread/jevent.h"
2627
#include "map.h"
2728
#include "environment.h"
2829
#include "util/container.h"
@@ -106,7 +107,6 @@ EmergeManager::EmergeManager(IGameDef *gamedef) {
106107

107108
mapgen_debug_info = g_settings->getBool("enable_mapgen_debug_info");
108109

109-
queuemutex.Init();
110110

111111
int nthreads;
112112
if (g_settings->get("num_emerge_threads").empty()) {
@@ -385,7 +385,7 @@ void EmergeManager::registerMapgen(std::string mgname, MapgenFactory *mgfactory)
385385
}
386386

387387

388-
////////////////////////////// Emerge Thread //////////////////////////////////
388+
////////////////////////////// Emerge Thread //////////////////////////////////
389389

390390
bool EmergeThread::popBlockEmerge(v3s16 *pos, u8 *flags) {
391391
std::map<v3s16, BlockEmergeData *>::iterator iter;
@@ -399,7 +399,7 @@ bool EmergeThread::popBlockEmerge(v3s16 *pos, u8 *flags) {
399399
*pos = p;
400400

401401
iter = emerge->blocks_enqueued.find(p);
402-
if (iter == emerge->blocks_enqueued.end())
402+
if (iter == emerge->blocks_enqueued.end())
403403
return false; //uh oh, queue and map out of sync!!
404404

405405
BlockEmergeData *bedata = iter->second;
@@ -414,11 +414,11 @@ bool EmergeThread::popBlockEmerge(v3s16 *pos, u8 *flags) {
414414
}
415415

416416

417-
bool EmergeThread::getBlockOrStartGen(v3s16 p, MapBlock **b,
417+
bool EmergeThread::getBlockOrStartGen(v3s16 p, MapBlock **b,
418418
BlockMakeData *data, bool allow_gen) {
419419
v2s16 p2d(p.X, p.Z);
420420
//envlock: usually takes <=1ms, sometimes 90ms or ~400ms to acquire
421-
JMutexAutoLock envlock(m_server->m_env_mutex);
421+
JMutexAutoLock envlock(m_server->m_env_mutex);
422422

423423
// Load sector if it isn't loaded
424424
if (map->getSectorNoGenerateNoEx(p2d) == NULL)
@@ -496,7 +496,7 @@ void *EmergeThread::Thread() {
496496

497497
{
498498
//envlock: usually 0ms, but can take either 30 or 400ms to acquire
499-
JMutexAutoLock envlock(m_server->m_env_mutex);
499+
JMutexAutoLock envlock(m_server->m_env_mutex);
500500
ScopeProfiler sp(g_profiler, "EmergeThread: after "
501501
"Mapgen::makeChunk (envlock)", SPT_AVG);
502502

@@ -513,7 +513,7 @@ void *EmergeThread::Thread() {
513513

514514
// Ignore map edit events, they will not need to be sent
515515
// to anybody because the block hasn't been sent to anybody
516-
MapEditEventAreaIgnorer
516+
MapEditEventAreaIgnorer
517517
ign(&m_server->m_ignore_map_edit_events_area,
518518
VoxelArea(minp, maxp));
519519
{ // takes about 90ms with -O1 on an e3-1230v2

‎src/jthread/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ if( UNIX )
33
${CMAKE_CURRENT_SOURCE_DIR}/pthread/jmutex.cpp
44
${CMAKE_CURRENT_SOURCE_DIR}/pthread/jthread.cpp
55
${CMAKE_CURRENT_SOURCE_DIR}/pthread/jsemaphore.cpp
6+
${CMAKE_CURRENT_SOURCE_DIR}/pthread/jevent.cpp
67
PARENT_SCOPE)
78
else( UNIX )
89
set(JTHREAD_SRCS
910
${CMAKE_CURRENT_SOURCE_DIR}/win32/jmutex.cpp
1011
${CMAKE_CURRENT_SOURCE_DIR}/win32/jthread.cpp
1112
${CMAKE_CURRENT_SOURCE_DIR}/win32/jsemaphore.cpp
13+
${CMAKE_CURRENT_SOURCE_DIR}/win32/jevent.cpp
1214
PARENT_SCOPE)
1315
endif( UNIX )

‎src/jthread/jevent.h

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
3+
This file is a part of the JThread package, which contains some object-
4+
oriented thread wrappers for different thread implementations.
5+
6+
Copyright (c) 2000-2006 Jori Liesenborgs (jori.liesenborgs@gmail.com)
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a
9+
copy of this software and associated documentation files (the "Software"),
10+
to deal in the Software without restriction, including without limitation
11+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
12+
and/or sell copies of the Software, and to permit persons to whom the
13+
Software is furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in
16+
all copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21+
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24+
DEALINGS IN THE SOFTWARE.
25+
26+
*/
27+
28+
#ifndef JEVENT_H_
29+
#define JEVENT_H_
30+
31+
#ifdef _WIN32
32+
#include <windows.h>
33+
#else
34+
#include <semaphore.h>
35+
#endif
36+
37+
38+
class Event {
39+
#ifdef _WIN32
40+
HANDLE hEvent;
41+
#else
42+
sem_t sem;
43+
#endif
44+
45+
public:
46+
Event();
47+
~Event();
48+
void wait();
49+
void signal();
50+
};
51+
52+
#endif /* JEVENT_H_ */

‎src/jthread/jmutex.h

-53
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,8 @@ class JMutex
5353
public:
5454
JMutex();
5555
~JMutex();
56-
int Init();
5756
int Lock();
5857
int Unlock();
59-
bool IsInitialized() { return initialized; }
6058

6159
private:
6260
#if (defined(WIN32) || defined(_WIN32_WCE))
@@ -76,57 +74,6 @@ class JMutex
7674
return false;
7775
}
7876
#endif // WIN32
79-
bool initialized;
8077
};
8178

82-
#ifdef _WIN32
83-
84-
class Event {
85-
HANDLE hEvent;
86-
87-
public:
88-
Event() {
89-
hEvent = CreateEvent(NULL, 0, 0, NULL);
90-
}
91-
92-
~Event() {
93-
CloseHandle(hEvent);
94-
}
95-
96-
void wait() {
97-
WaitForSingleObject(hEvent, INFINITE);
98-
}
99-
100-
void signal() {
101-
SetEvent(hEvent);
102-
}
103-
};
104-
105-
#else
106-
107-
#include <semaphore.h>
108-
109-
class Event {
110-
sem_t sem;
111-
112-
public:
113-
Event() {
114-
sem_init(&sem, 0, 0);
115-
}
116-
117-
~Event() {
118-
sem_destroy(&sem);
119-
}
120-
121-
void wait() {
122-
sem_wait(&sem);
123-
}
124-
125-
void signal() {
126-
sem_post(&sem);
127-
}
128-
};
129-
130-
#endif
131-
13279
#endif // JMUTEX_H

‎src/jthread/jthread.h

-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ class JThread
7474

7575
JMutex runningmutex;
7676
JMutex continuemutex,continuemutex2;
77-
bool mutexinit;
7877
};
7978

8079
#endif // JTHREAD_H

‎src/jthread/pthread/jevent.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
3+
This file is a part of the JThread package, which contains some object-
4+
oriented thread wrappers for different thread implementations.
5+
6+
Copyright (c) 2000-2006 Jori Liesenborgs (jori.liesenborgs@gmail.com)
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a
9+
copy of this software and associated documentation files (the "Software"),
10+
to deal in the Software without restriction, including without limitation
11+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
12+
and/or sell copies of the Software, and to permit persons to whom the
13+
Software is furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in
16+
all copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21+
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24+
DEALINGS IN THE SOFTWARE.
25+
26+
*/
27+
#include <assert.h>
28+
#include "jthread/jevent.h"
29+
30+
Event::Event() {
31+
assert(sem_init(&sem, 0, 0) == 0);
32+
}
33+
34+
Event::~Event() {
35+
assert(sem_destroy(&sem) == 0);
36+
}
37+
38+
void Event::wait() {
39+
assert(sem_wait(&sem) == 0);
40+
}
41+
42+
void Event::signal() {
43+
assert(sem_post(&sem) == 0);
44+
}

‎src/jthread/pthread/jmutex.cpp

+5-18
Original file line numberDiff line numberDiff line change
@@ -24,40 +24,27 @@
2424
DEALINGS IN THE SOFTWARE.
2525
2626
*/
27-
27+
#include <assert.h>
2828
#include "jthread/jmutex.h"
2929

3030
JMutex::JMutex()
3131
{
32-
pthread_mutex_init(&mutex,NULL);
33-
initialized = true;
32+
assert(pthread_mutex_init(&mutex,NULL) == 0);
3433
}
3534

3635
JMutex::~JMutex()
3736
{
38-
if (initialized)
39-
pthread_mutex_destroy(&mutex);
40-
}
41-
42-
int JMutex::Init()
43-
{
44-
return 0;
37+
assert(pthread_mutex_destroy(&mutex) == 0);
4538
}
4639

4740
int JMutex::Lock()
4841
{
49-
if (!initialized)
50-
return ERR_JMUTEX_NOTINIT;
51-
52-
pthread_mutex_lock(&mutex);
42+
assert(pthread_mutex_lock(&mutex) == 0);
5343
return 0;
5444
}
5545

5646
int JMutex::Unlock()
5747
{
58-
if (!initialized)
59-
return ERR_JMUTEX_NOTINIT;
60-
61-
pthread_mutex_unlock(&mutex);
48+
assert(pthread_mutex_unlock(&mutex) == 0);
6249
return 0;
6350
}

0 commit comments

Comments
 (0)
Please sign in to comment.