aboutsummaryrefslogtreecommitdiff
path: root/src/jthread/jevent.h
blob: 9ea7ebde87d21d5371791e221b0abb6e17e0b8bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*

    This file is a part of the JThread package, which contains some object-
    oriented thread wrappers for different thread implementations.

    Copyright (c) 2000-2006  Jori Liesenborgs (jori.liesenborgs@gmail.com)

    Permission is hereby granted, free of charge, to any person obtaining a
    copy of this software and associated documentation files (the "Software"),
    to deal in the Software without restriction, including without limitation
    the rights to use, copy, modify, merge, publish, distribute, sublicense,
    and/or sell copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
    THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    DEALINGS IN THE SOFTWARE.

*/

#ifndef JEVENT_H_
#define JEVENT_H_

#ifdef _WIN32
#include <windows.h>
#elif defined(__MACH__) && defined(__APPLE__)
#include <mach/mach.h>
#include <mach/task.h>
#include <mach/semaphore.h>
#include <sys/semaphore.h>
#else
#include <semaphore.h>
#endif


class Event {
#ifdef _WIN32
	HANDLE hEvent;
#elif defined(__MACH__) && defined(__APPLE__)
	semaphore_t sem;
#else
	sem_t sem;
#endif

public:
	Event();
	~Event();
	void wait();
	void signal();
};

#endif /* JEVENT_H_ */
='n185' href='#n185'>185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
/*
Minetest-c55
Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

/*
(c) 2010 Perttu Ahola <celeron55@gmail.com>
*/

#ifndef MAPSECTOR_HEADER
#define MAPSECTOR_HEADER

#include <jmutex.h>
#include "common_irrlicht.h"
#include "mapblock.h"
//#include "heightmap.h"
#include "exceptions.h"

/*
	This is an Y-wise stack of MapBlocks.
*/

#define MAPSECTOR_SERVER 0
#define MAPSECTOR_CLIENT 1

class MapSector: public NodeContainer
{
public:
	
	MapSector(NodeContainer *parent, v2s16 pos);
	virtual ~MapSector();

	virtual u16 nodeContainerId() const
	{
		return NODECONTAINER_ID_MAPSECTOR;
	}

	virtual u32 getId() const = 0;

	void deleteBlocks();

	v2s16 getPos()
	{
		return m_pos;
	}

	MapBlock * getBlockNoCreateNoEx(s16 y);
	MapBlock * getBlockNoCreate(s16 y);
	MapBlock * createBlankBlockNoInsert(s16 y);
	MapBlock * createBlankBlock(s16 y);
	//MapBlock * getBlock(s16 y, bool generate=true);

	void insertBlock(MapBlock *block);
	
	// This is used to remove a dummy from the sector while generating it.
	// Block is only removed from internal container, not deleted.
	void removeBlock(MapBlock *block);
	
	/*
		This might not be a thread-safe depending on the day.
		See the implementation.
	*/
	void getBlocks(core::list<MapBlock*> &dest);
	
	/*
		If all nodes in area can be accessed, returns true and
		adds all blocks in area to blocks.

		If all nodes in area cannot be accessed, returns false.

		The implementation of this is quite slow

		if blocks==NULL; it is not accessed at all.
	*/
	bool isValidArea(v3s16 p_min_nodes, v3s16 p_max_nodes,
			core::map<s16, MapBlock*> *blocks)
	{
		core::map<s16, MapBlock*> bs;
		
		v3s16 p_min = getNodeBlockPos(p_min_nodes);
		v3s16 p_max = getNodeBlockPos(p_max_nodes);
		if(p_min.X != 0 || p_min.Z != 0
				|| p_max.X != 0 || p_max.Z != 0)
			return false;
		v3s16 y;
		for(s16 y=p_min.Y; y<=p_max.Y; y++)
		{
			try{
				MapBlock *block = getBlockNoCreate(y);
				if(block->isDummy())
					return false;
				if(blocks!=NULL)
					bs[y] = block;
			}
			catch(InvalidPositionException &e)
			{
				return false;
			}
		}

		if(blocks!=NULL)
		{
			for(core::map<s16, MapBlock*>::Iterator i=bs.getIterator();
					i.atEnd()==false; i++)
			{
				MapBlock *block = i.getNode()->getValue();
				s16 y = i.getNode()->getKey();
				blocks->insert(y, block);
			}
		}
		return true;
	}

	void getBlocksInArea(v3s16 p_min_nodes, v3s16 p_max_nodes,
			core::map<v3s16, MapBlock*> &blocks)
	{
		v3s16 p_min = getNodeBlockPos(p_min_nodes);
		v3s16 p_max = getNodeBlockPos(p_max_nodes);
		v3s16 y;
		for(s16 y=p_min.Y; y<=p_max.Y; y++)
		{
			try{
				MapBlock *block = getBlockNoCreate(y);
				blocks.insert(block->getPos(), block);
			}
			catch(InvalidPositionException &e)
			{
			}
		}
	}
	
	// virtual from NodeContainer
	bool isValidPosition(v3s16 p)
	{
		v3s16 blockpos = getNodeBlockPos(p);

		if(blockpos.X != 0 || blockpos.Z != 0)
			return false;

		MapBlock *blockref;
		try{
			blockref = getBlockNoCreate(blockpos.Y);
		}
		catch(InvalidPositionException &e)
		{
			return false;
		}

		return true;
	}

	// virtual from NodeContainer
	MapNode getNode(v3s16 p)
	{
		v3s16 blockpos = getNodeBlockPos(p);
		if(blockpos.X != 0 || blockpos.Z != 0)
			throw InvalidPositionException
				("MapSector only allows Y");

		MapBlock * blockref = getBlockNoCreate(blockpos.Y);
		v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;

		return blockref->getNode(relpos);
	}
	// virtual from NodeContainer
	void setNode(v3s16 p, MapNode & n)
	{
		v3s16 blockpos = getNodeBlockPos(p);
		if(blockpos.X != 0 || blockpos.Z != 0)
			throw InvalidPositionException
				("MapSector only allows Y");

		MapBlock * blockref = getBlockNoCreate(blockpos.Y);
		v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
		blockref->setNode(relpos, n);
	}

	// DEPRECATED?
	virtual f32 getGroundHeight(v2s16 p, bool generate=false)
	{
		return GROUNDHEIGHT_NOTFOUND_SETVALUE;
	}
	virtual void setGroundHeight(v2s16 p, f32 y, bool generate=false)
	{
	}
	
	// When true, sector metadata is changed from the one on disk
	// (sector metadata = all but blocks)
	// Basically, this should be changed to true in every setter method
	bool differs_from_disk;

	// Counts seconds from last usage.
	// Sector can be deleted from memory after some time of inactivity.
	// NOTE: It has to be made very sure no other thread is accessing
	//       the sector and it doesn't remain in any cache when
	//       deleting it.
	float usage_timer;

protected:
	
	// The pile of MapBlocks
	core::map<s16, MapBlock*> m_blocks;
	//JMutex m_blocks_mutex; // For public access functions

	NodeContainer *m_parent;
	// Position on parent (in MapBlock widths)
	v2s16 m_pos;

	// Be sure to set this to NULL when the cached block is deleted 
	MapBlock *m_block_cache;
	s16 m_block_cache_y;
	
	// This is used for protecting m_blocks
	JMutex m_mutex;
	
	/*
		Private methods
	*/
	MapBlock *getBlockBuffered(s16 y);

};

class ServerMapSector : public MapSector
{
public:
	ServerMapSector(NodeContainer *parent, v2s16 pos);
	~ServerMapSector();
	
	u32 getId() const
	{
		return MAPSECTOR_SERVER;
	}
	
	// DEPRECATED?
	f32 getGroundHeight(v2s16 p, bool generate=false);
	void setGroundHeight(v2s16 p, f32 y, bool generate=false);

	/*
		These functions handle metadata.
		They do not handle blocks.
	*/