aboutsummaryrefslogtreecommitdiff
path: root/src/lanes.c
diff options
context:
space:
mode:
authorBenoit Germain <bnt.germain@gmail.com>2012-09-13 22:40:16 +0200
committerBenoit Germain <bnt.germain@gmail.com>2012-09-13 22:40:16 +0200
commit30b046f829541ae5ff8dc1088cea8e09bdc320db (patch)
treeb9d3e926b60aa0d88fafd477c94a23df6537321a /src/lanes.c
parent5fad772d922250db9d46edec156cd89939c4d8b5 (diff)
downloadlanes-30b046f829541ae5ff8dc1088cea8e09bdc320db.tar.gz
lanes-30b046f829541ae5ff8dc1088cea8e09bdc320db.tar.bz2
lanes-30b046f829541ae5ff8dc1088cea8e09bdc320db.zip
minor internal changes
* implemented set_debug_threadname() for pthread builds where possible * refactored linda __tostring and __concat * linda metatable's __metatable is a string instead of a boolean
Diffstat (limited to 'src/lanes.c')
-rw-r--r--src/lanes.c109
1 files changed, 35 insertions, 74 deletions
diff --git a/src/lanes.c b/src/lanes.c
index 3ac085d..eac4753 100644
--- a/src/lanes.c
+++ b/src/lanes.c
@@ -713,18 +713,31 @@ LUAG_FUNC( linda_deep ) {
713* 713*
714* Useful for concatenation or debugging purposes 714* Useful for concatenation or debugging purposes
715*/ 715*/
716LUAG_FUNC( linda_tostring) 716
717static int linda_tostring( lua_State* L, int _idx, bool_t _opt)
717{ 718{
718 char text[32]; 719 char text[32];
719 int len; 720 int len;
720 struct s_Linda* linda = lua_toLinda( L, 1); 721 struct s_Linda* linda = lua_toLinda( L, _idx);
721 luaL_argcheck( L, linda, 1, "expected a linda object!"); 722 if( !_opt)
722 if( linda->name[0]) 723 {
723 len = sprintf( text, "linda: %.*s", (int)sizeof(text) - 8, linda->name); 724 luaL_argcheck( L, linda, _idx, "expected a linda object!");
724 else 725 }
725 len = sprintf( text, "linda: %p", linda); 726 if( linda)
726 lua_pushlstring( L, text, len); 727 {
727 return 1; 728 if( linda->name[0])
729 len = sprintf( text, "Linda: %.*s", (int)sizeof(text) - 8, linda->name);
730 else
731 len = sprintf( text, "Linda: %p", linda);
732 lua_pushlstring( L, text, len);
733 return 1;
734 }
735 return 0;
736}
737
738LUAG_FUNC( linda_tostring)
739{
740 return linda_tostring( L, 1, FALSE);
728} 741}
729 742
730 743
@@ -736,35 +749,23 @@ LUAG_FUNC( linda_tostring)
736* Useful for concatenation or debugging purposes 749* Useful for concatenation or debugging purposes
737*/ 750*/
738LUAG_FUNC( linda_concat) 751LUAG_FUNC( linda_concat)
739{ 752{ // linda1? linda2?
740 struct s_Linda *linda1 = lua_toLinda( L, 1); 753 bool_t atLeastOneLinda = FALSE;
741 struct s_Linda *linda2 = lua_toLinda( L, 2); 754 // Lua semantics enforce that one of the 2 arguments is a Linda, but not necessarily both.
742 // lua semantics should enforce that one of the parameters we got is a linda 755 if( linda_tostring( L, 1, TRUE))
743 luaL_argcheck( L, linda1 || linda2, 1, "expected a linda object!");
744 // replace the lindas by their string equivalents in the stack
745 if ( linda1)
746 { 756 {
747 char text[32]; 757 atLeastOneLinda = TRUE;
748 int len;
749 if( linda1->name[0])
750 len = sprintf( text, "linda: %.*s", (int)sizeof(text) - 8, linda1->name);
751 else
752 len = sprintf( text, "linda: %p", linda1);
753 lua_pushlstring( L, text, len);
754 lua_replace( L, 1); 758 lua_replace( L, 1);
755 } 759 }
756 if ( linda2) 760 if( linda_tostring( L, 2, TRUE))
757 { 761 {
758 char text[32]; 762 atLeastOneLinda = TRUE;
759 int len;
760 if( linda2->name[0])
761 len = sprintf( text, "linda: %.*s", (int)sizeof(text) - 8, linda2->name);
762 else
763 len = sprintf( text, "linda: %p", linda2);
764 lua_pushlstring( L, text, len);
765 lua_replace( L, 2); 763 lua_replace( L, 2);
766 } 764 }
767 // concat the result 765 if( !atLeastOneLinda) // should not be possible
766 {
767 return luaL_error( L, "internal error: linda_concat called on non-Linda");
768 }
768 lua_concat( L, 2); 769 lua_concat( L, 2);
769 return 1; 770 return 1;
770} 771}
@@ -853,7 +854,7 @@ static void linda_id( lua_State *L, char const * const which)
853 lua_setfield( L, -2, "__index"); 854 lua_setfield( L, -2, "__index");
854 855
855 // protect metatable from external access 856 // protect metatable from external access
856 lua_pushboolean( L, 0); 857 lua_pushliteral( L, "Linda");
857 lua_setfield( L, -2, "__metatable"); 858 lua_setfield( L, -2, "__metatable");
858 859
859 lua_pushcfunction( L, LG_linda_tostring); 860 lua_pushcfunction( L, LG_linda_tostring);
@@ -1519,52 +1520,12 @@ static int lane_error( lua_State* L)
1519} 1520}
1520#endif // ERROR_FULL_STACK 1521#endif // ERROR_FULL_STACK
1521 1522
1522#if defined PLATFORM_WIN32 && !defined __GNUC__
1523//see http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
1524#define MS_VC_EXCEPTION 0x406D1388
1525#pragma pack(push,8)
1526typedef struct tagTHREADNAME_INFO
1527{
1528 DWORD dwType; // Must be 0x1000.
1529 LPCSTR szName; // Pointer to name (in user addr space).
1530 DWORD dwThreadID; // Thread ID (-1=caller thread).
1531 DWORD dwFlags; // Reserved for future use, must be zero.
1532} THREADNAME_INFO;
1533#pragma pack(pop)
1534
1535void SetThreadName( DWORD dwThreadID, char const *_threadName)
1536{
1537 THREADNAME_INFO info;
1538 Sleep(10);
1539 info.dwType = 0x1000;
1540 info.szName = _threadName;
1541 info.dwThreadID = dwThreadID;
1542 info.dwFlags = 0;
1543
1544 __try
1545 {
1546 RaiseException( MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info );
1547 }
1548 __except(EXCEPTION_EXECUTE_HANDLER)
1549 {
1550 }
1551}
1552#endif
1553
1554LUAG_FUNC( set_debug_threadname) 1523LUAG_FUNC( set_debug_threadname)
1555{ 1524{
1556 luaL_checktype( L, -1, LUA_TSTRING); 1525 luaL_checktype( L, -1, LUA_TSTRING);
1557#if defined PLATFORM_WIN32 && !defined __GNUC__ 1526 THREAD_SETNAME( lua_tostring( L, -1));
1558 {
1559 char const *threadName = lua_tostring( L, -1);
1560
1561 // to see thead name in Visual Studio C debugger
1562 SetThreadName(-1, threadName);
1563 }
1564#endif // defined PLATFORM_WIN32 && !defined __GNUC__
1565 // to see VM name in Decoda debugger Virtual Machine window 1527 // to see VM name in Decoda debugger Virtual Machine window
1566 lua_setglobal( L, "decoda_name"); 1528 lua_setglobal( L, "decoda_name");
1567
1568 return 0; 1529 return 0;
1569} 1530}
1570 1531