diff options
author | benoit-germain <bnt.germain@gmail.com> | 2012-08-06 19:59:38 +0300 |
---|---|---|
committer | benoit-germain <bnt.germain@gmail.com> | 2012-08-06 19:59:38 +0300 |
commit | 7e5ebfcc61bd911b716eef957306db2b9a45b162 (patch) | |
tree | 47703f2799016dae68c33ec96998dbbaf271175b /src/lanes.c | |
parent | eb1d18b88bfe572c034cc918210ce5ac93e01847 (diff) | |
download | lanes-7e5ebfcc61bd911b716eef957306db2b9a45b162.tar.gz lanes-7e5ebfcc61bd911b716eef957306db2b9a45b162.tar.bz2 lanes-7e5ebfcc61bd911b716eef957306db2b9a45b162.zip |
lanes.linda() accepts an optional name argument
Diffstat (limited to 'src/lanes.c')
-rw-r--r-- | src/lanes.c | 39 |
1 files changed, 33 insertions, 6 deletions
diff --git a/src/lanes.c b/src/lanes.c index 300c924..b65c6fa 100644 --- a/src/lanes.c +++ b/src/lanes.c | |||
@@ -238,6 +238,7 @@ static bool_t push_registry_table( lua_State *L, void *key, bool_t create ) { | |||
238 | struct s_Linda { | 238 | struct s_Linda { |
239 | SIGNAL_T read_happened; | 239 | SIGNAL_T read_happened; |
240 | SIGNAL_T write_happened; | 240 | SIGNAL_T write_happened; |
241 | char name[1]; | ||
241 | }; | 242 | }; |
242 | 243 | ||
243 | static void linda_id( lua_State*, char const * const which); | 244 | static void linda_id( lua_State*, char const * const which); |
@@ -711,7 +712,10 @@ LUAG_FUNC( linda_tostring) | |||
711 | int len; | 712 | int len; |
712 | struct s_Linda* linda = lua_toLinda( L, 1); | 713 | struct s_Linda* linda = lua_toLinda( L, 1); |
713 | luaL_argcheck( L, linda, 1, "expected a linda object!"); | 714 | luaL_argcheck( L, linda, 1, "expected a linda object!"); |
714 | len = sprintf( text, "linda: %p", linda); | 715 | if( linda->name[0]) |
716 | len = sprintf( text, "linda: %.*s", sizeof(text) - 8, linda->name); | ||
717 | else | ||
718 | len = sprintf( text, "linda: %p", linda); | ||
715 | lua_pushlstring( L, text, len); | 719 | lua_pushlstring( L, text, len); |
716 | return 1; | 720 | return 1; |
717 | } | 721 | } |
@@ -734,14 +738,22 @@ LUAG_FUNC( linda_concat) | |||
734 | if ( linda1) | 738 | if ( linda1) |
735 | { | 739 | { |
736 | char text[32]; | 740 | char text[32]; |
737 | int len = sprintf( text, "linda: %p", linda1); | 741 | int len; |
742 | if( linda1->name[0]) | ||
743 | len = sprintf( text, "linda: %.*s", sizeof(text) - 8, linda1->name); | ||
744 | else | ||
745 | len = sprintf( text, "linda: %p", linda1); | ||
738 | lua_pushlstring( L, text, len); | 746 | lua_pushlstring( L, text, len); |
739 | lua_replace( L, 1); | 747 | lua_replace( L, 1); |
740 | } | 748 | } |
741 | if ( linda2) | 749 | if ( linda2) |
742 | { | 750 | { |
743 | char text[32]; | 751 | char text[32]; |
744 | int len = sprintf( text, "linda: %p", linda2); | 752 | int len; |
753 | if( linda2->name[0]) | ||
754 | len = sprintf( text, "linda: %.*s", sizeof(text) - 8, linda2->name); | ||
755 | else | ||
756 | len = sprintf( text, "linda: %p", linda2); | ||
745 | lua_pushlstring( L, text, len); | 757 | lua_pushlstring( L, text, len); |
746 | lua_replace( L, 2); | 758 | lua_replace( L, 2); |
747 | } | 759 | } |
@@ -779,16 +791,26 @@ static void linda_id( lua_State *L, char const * const which) | |||
779 | if (strcmp( which, "new" )==0) | 791 | if (strcmp( which, "new" )==0) |
780 | { | 792 | { |
781 | struct s_Linda *s; | 793 | struct s_Linda *s; |
794 | size_t name_len = 0; | ||
795 | char const* linda_name = NULL; | ||
796 | |||
797 | if( lua_type( L, lua_gettop( L)) == LUA_TSTRING) | ||
798 | { | ||
799 | linda_name = lua_tostring( L, lua_gettop( L)); | ||
800 | name_len = strlen( linda_name); | ||
801 | } | ||
782 | 802 | ||
783 | /* The deep data is allocated separately of Lua stack; we might no | 803 | /* The deep data is allocated separately of Lua stack; we might no |
784 | * longer be around when last reference to it is being released. | 804 | * longer be around when last reference to it is being released. |
785 | * One can use any memory allocation scheme. | 805 | * One can use any memory allocation scheme. |
786 | */ | 806 | */ |
787 | s= (struct s_Linda *) malloc( sizeof(struct s_Linda) ); | 807 | s= (struct s_Linda *) malloc( sizeof(struct s_Linda) + name_len); // terminating 0 is already included |
788 | ASSERT_L(s); | 808 | ASSERT_L(s); |
789 | 809 | ||
790 | SIGNAL_INIT( &s->read_happened ); | 810 | SIGNAL_INIT( &s->read_happened ); |
791 | SIGNAL_INIT( &s->write_happened ); | 811 | SIGNAL_INIT( &s->write_happened ); |
812 | s->name[0] = 0; | ||
813 | memcpy( s->name, linda_name, name_len ? name_len + 1 : 0); | ||
792 | 814 | ||
793 | lua_pushlightuserdata( L, s ); | 815 | lua_pushlightuserdata( L, s ); |
794 | } | 816 | } |
@@ -879,6 +901,10 @@ static void linda_id( lua_State *L, char const * const which) | |||
879 | */ | 901 | */ |
880 | LUAG_FUNC( linda) | 902 | LUAG_FUNC( linda) |
881 | { | 903 | { |
904 | int const top = lua_gettop( L); | ||
905 | luaL_argcheck( L, top <= 1, top, "too many arguments"); | ||
906 | if( top == 1) | ||
907 | luaL_checktype( L, 1, LUA_TSTRING); | ||
882 | return luaG_deep_userdata( L, linda_id); | 908 | return luaG_deep_userdata( L, linda_id); |
883 | } | 909 | } |
884 | 910 | ||
@@ -2376,7 +2402,10 @@ static void init_once_LOCKED( lua_State* L, volatile DEEP_PRELUDE** timer_deep_r | |||
2376 | { | 2402 | { |
2377 | // proxy_ud= deep_userdata( idfunc ) | 2403 | // proxy_ud= deep_userdata( idfunc ) |
2378 | // | 2404 | // |
2405 | lua_pushliteral( L, "lanes-timer"); // push a name for debug purposes | ||
2379 | luaG_deep_userdata( L, linda_id); | 2406 | luaG_deep_userdata( L, linda_id); |
2407 | STACK_MID( L, 2) | ||
2408 | lua_remove( L, -2); // remove the name as we no longer need it | ||
2380 | 2409 | ||
2381 | ASSERT_L( lua_isuserdata(L,-1) ); | 2410 | ASSERT_L( lua_isuserdata(L,-1) ); |
2382 | 2411 | ||
@@ -2533,5 +2562,3 @@ luaopen_lanes_core( lua_State *L ) | |||
2533 | STACK_END( L, 1) | 2562 | STACK_END( L, 1) |
2534 | return 1; | 2563 | return 1; |
2535 | } | 2564 | } |
2536 | |||
2537 | |||