File: LuaUser.cpp

package info (click to toggle)
spring 103.0%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,720 kB
  • ctags: 63,685
  • sloc: cpp: 368,283; ansic: 33,988; python: 12,417; java: 12,203; awk: 5,879; sh: 1,846; xml: 655; perl: 405; php: 211; objc: 194; makefile: 77; sed: 2
file content (487 lines) | stat: -rw-r--r-- 12,259 bytes parent folder | download
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include <map>
#include <array>
#include <boost/cstdint.hpp>
#include <boost/thread.hpp>
#include "lib/streflop/streflop_cond.h"

#include "LuaInclude.h"
#include "Game/GameVersion.h"
#include "Lua/LuaHandle.h"
#include "System/myMath.h"
#include "System/Platform/Threading.h"
#include "System/Threading/SpringMutex.h"
#include "System/Log/ILog.h"
#if (!defined(DEDICATED) && !defined(UNITSYNC) && !defined(BUILDING_AI))
	#include "System/Misc/SpringTime.h"
#endif


///////////////////////////////////////////////////////////////////////////
// Custom Lua Mutexes

static std::map<lua_State*, spring::recursive_mutex*> mutexes;
static std::map<lua_State*, bool> coroutines;

static spring::recursive_mutex* GetLuaMutex(lua_State* L)
{
	assert(!mutexes[L]);
	return new spring::recursive_mutex();
}



void LuaCreateMutex(lua_State* L)
{
	#if (ENABLE_USERSTATE_LOCKS == 0)
	// if LoadingMT=1, everything runs in the game-load thread (on startup)
	//assert(Threading::IsMainThread() || Threading::IsGameLoadThread() || SpringVersion::IsUnitsync());
	return;
	#endif

	luaContextData* lcd = GetLuaContextData(L);
	if (!lcd) return; // CLuaParser
	assert(lcd);

	spring::recursive_mutex* mutex = GetLuaMutex(L);
	lcd->luamutex = mutex;
	mutexes[L] = mutex;
}


void LuaDestroyMutex(lua_State* L)
{
	#if (ENABLE_USERSTATE_LOCKS == 0)
	//assert(Threading::IsMainThread() || Threading::IsGameLoadThread() || SpringVersion::IsUnitsync());
	return;
	#endif

	if (!GetLuaContextData(L)) return; // CLuaParser
	assert(GetLuaContextData(L));

	if (coroutines.find(L) != coroutines.end()) {
		mutexes.erase(L);
		coroutines.erase(L);
	} else {
		lua_unlock(L);
		assert(mutexes.find(L) != mutexes.end());
		spring::recursive_mutex* mutex = GetLuaContextData(L)->luamutex;
		assert(mutex);
		delete mutex;
		mutexes.erase(L);
		//TODO erase all related coroutines too?
	}
}


void LuaLinkMutex(lua_State* L_parent, lua_State* L_child)
{
	#if (ENABLE_USERSTATE_LOCKS == 0)
	//assert(Threading::IsMainThread() || Threading::IsGameLoadThread() || SpringVersion::IsUnitsync());
	return;
	#endif

	luaContextData* plcd = GetLuaContextData(L_parent);
	assert(plcd);

	luaContextData* clcd = GetLuaContextData(L_child);
	assert(clcd);

	assert(plcd == clcd);

	coroutines[L_child] = true;
	mutexes[L_child] = plcd->luamutex;
}


void LuaMutexLock(lua_State* L)
{
	#if (ENABLE_USERSTATE_LOCKS == 0)
	//assert(Threading::IsMainThread() || Threading::IsGameLoadThread() || SpringVersion::IsUnitsync());
	return;
	#endif

	if (!GetLuaContextData(L)) return; // CLuaParser

	spring::recursive_mutex* mutex = GetLuaContextData(L)->luamutex;

	if (mutex->try_lock())
		return;

	//static int failedLocks = 0;
	//LOG("LuaMutexLock %i", ++failedLocks);
	mutex->lock();
}


void LuaMutexUnlock(lua_State* L)
{
	#if (ENABLE_USERSTATE_LOCKS == 0)
	//assert(Threading::IsMainThread() || Threading::IsGameLoadThread() || SpringVersion::IsUnitsync());
	return;
	#endif

	if (!GetLuaContextData(L)) return; // CLuaParser

	spring::recursive_mutex* mutex = GetLuaContextData(L)->luamutex;
	mutex->unlock();
}


void LuaMutexYield(lua_State* L)
{
	#if (ENABLE_USERSTATE_LOCKS == 0)
	//assert(Threading::IsMainThread() || Threading::IsGameLoadThread() || SpringVersion::IsUnitsync());
	return;
	#endif

	assert(GetLuaContextData(L));
	/*mutexes[L]->unlock();
	if (!mutexes[L]->try_lock()) {
		// only yield if another thread is waiting for the mutex
		boost::this_thread::yield();
		mutexes[L]->lock();
	}*/

	static int count = 0;
	bool y = false;
	if (count-- <= 0) { y = true; count = 30; }
	LuaMutexUnlock(L);

	if (y) boost::this_thread::yield();
	LuaMutexLock(L);
}


///////////////////////////////////////////////////////////////////////////
//

const char* spring_lua_getName(lua_State* L)
{
	auto ld = GetLuaContextData(L);
	if (ld) {
		return ld->owner->GetName().c_str();
	}

	static const char* c = "";
	return c;
}


///////////////////////////////////////////////////////////////////////////
// Custom Memory Allocator
//
// these track allocations across all states
static Threading::AtomicCounterInt64 totalBytesAlloced = 0;
static Threading::AtomicCounterInt64 totalNumLuaAllocs = 0;
static Threading::AtomicCounterInt64 totalLuaAllocTime = 0;

static const unsigned int maxAllocedBytes = 768u * 1024u*1024u;
static const char* maxAllocFmtStr = "%s: cannot allocate more memory! (%u bytes already used, %u bytes maximum)";


void* spring_lua_alloc(void* ud, void* ptr, size_t osize, size_t nsize)
{
	auto lcd = (luaContextData*) ud;

	if (nsize == 0) {
		totalBytesAlloced -= osize;
		free(ptr);
		return NULL;
	}

	if ((nsize > osize) && (totalBytesAlloced > maxAllocedBytes)) {
		// better kill Lua than whole engine
		// NOTE: this will trigger luaD_throw --> exit(EXIT_FAILURE)
		LOG_L(L_FATAL, maxAllocFmtStr, (lcd->owner->GetName()).c_str(), (unsigned int) totalBytesAlloced, maxAllocedBytes);
		return NULL;
	}

	#if (!defined(DEDICATED) && !defined(UNITSYNC) && !defined(BUILDING_AI))
	const spring_time t0 = spring_gettime();
	void* mem = realloc(ptr, nsize);
	const spring_time t1 = spring_gettime();

	totalBytesAlloced += (nsize - osize);
	totalNumLuaAllocs += 1;
	totalLuaAllocTime += (t1 - t0).toMicroSecsi();

	return mem;
	#else
	return (realloc(ptr, nsize));
	#endif
}

void spring_lua_alloc_get_stats(SLuaInfo* info)
{
	info->allocedBytes = totalBytesAlloced;
	info->numLuaAllocs = totalNumLuaAllocs;
	info->luaAllocTime = totalLuaAllocTime;
	info->numLuaStates = mutexes.size() - coroutines.size();
}

void spring_lua_alloc_update_stats(bool clear)
{
	if (clear) {
		totalNumLuaAllocs = 0;
		totalLuaAllocTime = 0;
	}
}

//////////////////////////////////////////////////////////
////// Custom synced float to string
//////////////////////////////////////////////////////////

#ifdef WIN32
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat"
static inline int sprintf64(char* dst, boost::int64_t x) { return sprintf(dst, "%I64d", x); }
#pragma GCC diagnostic pop
#else
static inline int sprintf64(char* dst, long int x)      { return sprintf(dst, "%ld", x); }
static inline int sprintf64(char* dst, long long int x) { return sprintf(dst, "%lld", x); }
#endif

// excluding mantissa, a float has a rest int-precision of: 2^24 = 16,777,216
// int numbers in that range are 100% exact, and don't suffer float precision issues
static constexpr int MAX_PRECISE_DIGITS_IN_FLOAT = std::numeric_limits<float>::digits10;
static constexpr auto SPRING_FLOAT_MAX = std::numeric_limits<float>::max();
static constexpr auto SPRING_INT64_MAX = std::numeric_limits<boost::int64_t>::max();

static constexpr std::array<double, 11> v = {
	1, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10
};


static constexpr inline double Pow10d(unsigned i)
{
	return (i<v.size()) ? v[i] : std::pow(double(10), i);
}


static const inline int FastLog10(const float f)
{
	assert(f != 0.0f); // log10(0) = -inf
	if (f>=1.f && f<(SPRING_INT64_MAX >> 1)) {
		const boost::int64_t i = f;
		int log10 = 0;
		boost::int64_t n = 10;
		while (i >= n) {
			++log10;
			n *= 10;
		}
		return log10;
	}
	return std::floor(std::log10(f));
}


static constexpr inline int GetDigitsInStdNotation(const int log10)
{
	// log10(0.01) = -2  (4 chars)
	// log10(0.1)  = -1  (3 chars)
	// log10(1)    = 0   (1 char)
	// log10(10)   = 1   (2 chars)
	// log10(100)  = 2   (3 chars)
	return (log10 >= 0) ? (log10 + 1) : (-log10 + 2);
}



static inline int PrintIntPart(char* buf, float f, const bool carrierBit = false)
{
#ifdef WIN32
	if (f < (std::numeric_limits<int>::max() - carrierBit)) {
		return sprintf(buf, "%d", int(f) + carrierBit);
	} else
#endif
	if (f < (SPRING_INT64_MAX - carrierBit)) {
		return sprintf64(buf, boost::int64_t(f) + carrierBit); // much faster than printing a float!
	} else {
		return sprintf(buf, "%1.0f", f + carrierBit);
	}
}


static inline int PrintFractPart(char* buf, float f, int digits, int precision)
{
	//XXX: Hacks, with streflop enabled we limit the FPU normally to 32bit float
	//     and doing any double math will use floats math then!
	//     But here we need the precision of doubles, so switch the FPU to it just
	//     for this casting.
	//Note: We are still in synced code, so even these doubles need to sync!
	//     Also performance seems to be unaffected by switching the FPU mode.
	streflop::streflop_init<streflop::Double>();

	const auto old = buf;

	assert(digits <= 15);
	assert(digits <= std::numeric_limits<boost::int64_t>::digits10);
	const boost::int64_t i = double(f) * Pow10d(digits) + 0.5;
	char s[16];
	const int len = sprintf64(s, i);
	if (len < digits) {
		memset(buf, '0', digits - len);
		buf += digits - len;
	}
	memcpy(buf, s, len);
	buf += len;

	// removing trailing zeros
	precision = std::max(1, precision);
	while (buf[-1] == '0' && (buf - old) > precision) --buf;
	buf[0] = '\0';

	streflop::streflop_init<streflop::Simple>();
	return (buf - old);
}


static inline bool HandleRounding(float* fractF, int log10, int charsInStdNotation, int nDigits, bool scienceNotation, int precision)
{
	// We handle here the case when rounding in the
	// fract part carries into the integer part.
	// We don't handle the fract rounding itself!

	int iDigits = 1;
	if (!scienceNotation) {
		if (log10 >= 0) {
			iDigits = charsInStdNotation;
		}
	}

	int fDigits = std::max(0, nDigits - (iDigits + 1)); // excluding dot
	if (precision >= 0) {
		fDigits = precision;
	}

	// 1 -> 0.95   -%.1f-> 1.0
	// 2 -> 0.995  -%.2f-> 1.00
	// 3 -> 0.9995 -%.3f-> 1.000
	const float roundLimit = 1.f - 0.5f * std::pow(0.1f, fDigits);
	if (*fractF >= roundLimit) {
		*fractF = 0.0f;
		return true;
	}
	return false;
}


void spring_lua_ftoa(float f, char* buf, int precision)
{
	static constexpr int MAX_DIGITS = 10;
	static_assert(MAX_DIGITS > 6, "must have enough room for at least 1.0e+23");

	// get rid of integers
	int x = f;
	if (float(x) == f) {
		sprintf(buf, "%i", x);
		if (precision > 0) {
			char* endBuf = strchr(buf, '\0');
			*endBuf = '.'; ++endBuf;
			memset(endBuf, '0', precision);
			endBuf[precision] = '\0';
		}
		return;
	}


	int nDigits = MAX_DIGITS;
	if (std::signbit(f)) { // use signbit() cause < doesn't work with nans
		f = -f;
		buf[0] = '-';
		++buf;
		--nDigits;
	}

	if (std::isinf(f)) {
		strcpy(buf, "inf");
		return;
	}
	if (std::isnan(f)) {
		strcpy(buf, "nan");
		return;
	}

	int e10 = 0;
	const int log10 = FastLog10(f);
	const int charsInStdNotation = GetDigitsInStdNotation(log10);
	if ((charsInStdNotation > nDigits) && (precision == -1)) {
		e10 = log10;
		nDigits -= 4; // space needed for "e+01"
		f *= std::pow(10.f, -e10);
	}
	const bool scienceNotation = (e10 != 0);

	float truncF;
	float fractF = std::modf(f, &truncF);
	const bool carrierBit = HandleRounding(&fractF, log10, charsInStdNotation, nDigits, scienceNotation, precision);

	const int iDigits = PrintIntPart(buf, truncF, carrierBit);
	if (scienceNotation)
		assert(iDigits == 1);
	nDigits -= iDigits;
	buf += iDigits;

	if (precision >= 0)
		nDigits = precision + 1; //+1 for dot
	if ((nDigits > 1) && (scienceNotation || fractF != 0 || precision > 0)) {
		buf[0] = '.';
		++buf;
		--nDigits;

		const int fDigits = PrintFractPart(buf, fractF, nDigits, precision);
		assert(fDigits >= 1);
		buf += fDigits;
	}

	if (scienceNotation) {
		sprintf(buf, "e%+02d", e10);
	}
}


void spring_lua_format(float f, const char* fmt, char* buf)
{
	if (fmt[0] == '\0')
		return spring_lua_ftoa(f, buf);

	// handles `%(sign)(width)(.precision)f`, i.e. %+10.2f

	char bufC[128];
	char* buf2 = bufC;

	// sign
	if (fmt[0] == '+' || fmt[0] == ' ') {
		if (!std::signbit(f)) { // use signbit() cause < doesn't work with nans
			buf2[0] = fmt[0];
			++buf2;
		}
		++fmt;
	}

	// width
	const int width = atoi(fmt);

	// precision
	int precision = -1;
	const char* dotPos = strchr(fmt, '.');
	if (dotPos != nullptr) {
		fmt = dotPos + 1;
		precision = Clamp(atoi(fmt), 0, 15);
	}

	// convert the float
	spring_lua_ftoa(f, buf2, precision);

	// right align the number when `width` is given
	const int len = strlen(bufC);
	if (len < width) {
		memset(buf, ' ', width - len);
		buf += width - len;
	}

	// copy the float string into dst
	memcpy(buf, bufC, len+1);
}