aboutsummaryrefslogtreecommitdiff
path: root/src/lanes.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lanes.c')
-rw-r--r--src/lanes.c136
1 files changed, 133 insertions, 3 deletions
diff --git a/src/lanes.c b/src/lanes.c
index 0c943aa..e06d367 100644
--- a/src/lanes.c
+++ b/src/lanes.c
@@ -537,6 +537,33 @@ LUAG_FUNC( linda_set)
537 537
538 538
539/* 539/*
540* [val]= linda_keys( linda_ud)
541*
542* Get the list of keys with pending data in the linda
543*/
544LUAG_FUNC( linda_keys)
545{
546 struct s_Linda *linda= lua_toLinda( L, 1);
547 int pushed;
548 luaL_argcheck( L, linda, 1, "expected a linda object!");
549
550 {
551 struct s_Keeper *K = keeper_acquire( linda);
552 pushed = keeper_call( K->L, "keys", L, linda, 2);
553 ASSERT_L( pushed==0 || pushed==1 );
554 keeper_release(K);
555 // must trigger error after keeper state has been released
556 if( pushed < 0)
557 {
558 luaL_error( L, "tried to copy unsupported types");
559 }
560 }
561
562 return pushed;
563}
564
565
566/*
540* [val]= linda_get( linda_ud, key_num|str|bool|lightuserdata ) 567* [val]= linda_get( linda_ud, key_num|str|bool|lightuserdata )
541* 568*
542* Get a value from Linda. 569* Get a value from Linda.
@@ -608,6 +635,57 @@ LUAG_FUNC( linda_deep ) {
608 635
609 636
610/* 637/*
638* string = linda:__tostring( linda_ud)
639*
640* Return the stringification of a linda
641*
642* Useful for concatenation or debugging purposes
643*/
644LUAG_FUNC( linda_tostring)
645{
646 char text[32];
647 struct s_Linda *linda = lua_toLinda( L, 1);
648 luaL_argcheck( L, linda, 1, "expected a linda object!");
649 sprintf( text, "linda: %p", linda);
650 lua_pushstring( L, text);
651 return 1;
652}
653
654
655/*
656* string = linda:__concat( a, b)
657*
658* Return the concatenation of a pair of items, one of them being a linda
659*
660* Useful for concatenation or debugging purposes
661*/
662LUAG_FUNC( linda_concat)
663{
664 struct s_Linda *linda1 = lua_toLinda( L, 1);
665 struct s_Linda *linda2 = lua_toLinda( L, 2);
666 // lua semantics should enforce that one of the parameters we got is a linda
667 luaL_argcheck( L, linda1 || linda2, 1, "expected a linda object!");
668 // replace the lindas by their string equivalents in the stack
669 if ( linda1)
670 {
671 char text[32];
672 sprintf( text, "linda: %p", linda1);
673 lua_pushstring( L, text);
674 lua_replace( L, 1);
675 }
676 if ( linda2)
677 {
678 char text[32];
679 sprintf( text, "linda: %p", linda2);
680 lua_pushstring( L, text);
681 lua_replace( L, 2);
682 }
683 // concat the result
684 lua_concat( L, 2);
685 return 1;
686}
687
688/*
611* Identity function of a shared userdata object. 689* Identity function of a shared userdata object.
612* 690*
613* lightuserdata= linda_id( "new" [, ...] ) 691* lightuserdata= linda_id( "new" [, ...] )
@@ -678,17 +756,28 @@ static void linda_id( lua_State *L, char const * const which)
678 // metatable is its own index 756 // metatable is its own index
679 lua_pushvalue( L, -1); 757 lua_pushvalue( L, -1);
680 lua_setfield( L, -2, "__index"); 758 lua_setfield( L, -2, "__index");
759
681 // protect metatable from external access 760 // protect metatable from external access
682 lua_pushboolean( L, 0); 761 lua_pushboolean( L, 0);
683 lua_setfield( L, -2, "__metatable"); 762 lua_setfield( L, -2, "__metatable");
763
764 lua_pushcfunction( L, LG_linda_tostring);
765 lua_setfield( L, -2, "__tostring");
766
767 lua_pushcfunction( L, LG_linda_concat);
768 lua_setfield( L, -2, "__concat");
769
684 // 770 //
685 // [-1]: linda metatable 771 // [-1]: linda metatable
686 lua_pushcfunction( L, LG_linda_send ); 772 lua_pushcfunction( L, LG_linda_send );
687 lua_setfield( L, -2, "send" ); 773 lua_setfield( L, -2, "send" );
688 774
689 lua_pushcfunction( L, LG_linda_receive ); 775 lua_pushcfunction( L, LG_linda_receive );
690 lua_setfield( L, -2, "receive" ); 776 lua_setfield( L, -2, "receive" );
691 777
778 lua_pushcfunction( L, LG_linda_keys );
779 lua_setfield( L, -2, "keys" );
780
692 lua_pushcfunction( L, LG_linda_limit ); 781 lua_pushcfunction( L, LG_linda_limit );
693 lua_setfield( L, -2, "limit" ); 782 lua_setfield( L, -2, "limit" );
694 783
@@ -1359,6 +1448,7 @@ LUAG_FUNC( set_debug_threadname)
1359// [cancelstep_uint=0], 1448// [cancelstep_uint=0],
1360// [prio_int=0], 1449// [prio_int=0],
1361// [globals_tbl], 1450// [globals_tbl],
1451// [packagepath],
1362// [... args ...] ) 1452// [... args ...] )
1363// 1453//
1364// Upvalues: metatable to use for 'lane_ud' 1454// Upvalues: metatable to use for 'lane_ud'
@@ -1373,8 +1463,10 @@ LUAG_FUNC( thread_new )
1373 uint_t cs= luaG_optunsigned( L, 3,0); 1463 uint_t cs= luaG_optunsigned( L, 3,0);
1374 int prio= (int)luaL_optinteger( L, 4,0); 1464 int prio= (int)luaL_optinteger( L, 4,0);
1375 uint_t glob= luaG_isany(L,5) ? 5:0; 1465 uint_t glob= luaG_isany(L,5) ? 5:0;
1466 uint_t ppath = luaG_isany(L,6) ? 6:0;
1467 uint_t pcpath = luaG_isany(L,7) ? 7:0;
1376 1468
1377#define FIXED_ARGS (5) 1469#define FIXED_ARGS (7)
1378 uint_t args= lua_gettop(L) - FIXED_ARGS; 1470 uint_t args= lua_gettop(L) - FIXED_ARGS;
1379 1471
1380 if (prio < THREAD_PRIO_MIN || prio > THREAD_PRIO_MAX) 1472 if (prio < THREAD_PRIO_MIN || prio > THREAD_PRIO_MAX)
@@ -1424,6 +1516,44 @@ LUAG_FUNC( thread_new )
1424 serialize_require( L2 ); 1516 serialize_require( L2 );
1425 } 1517 }
1426 1518
1519 // package.path
1520 STACK_CHECK(L2)
1521 if( ppath)
1522 {
1523 if (lua_type(L,ppath) != LUA_TSTRING)
1524 luaL_error( L, "expected packagepath as string, got %s", luaG_typename(L,ppath));
1525 lua_getglobal( L2, "package");
1526 if( lua_isnil( L2, -1))
1527 {
1528 lua_pop( L2, 1);
1529 luaL_error( L, "specifying a new path for packages, but lane doesn't load package library");
1530 }
1531 lua_pushvalue( L, ppath);
1532 luaG_inter_move( L, L2, 1); // moves the new path to L2
1533 lua_setfield( L2, -2, "path"); // set package.path
1534 lua_pop( L2, 1);
1535 }
1536 STACK_END(L2,0)
1537
1538 // package.cpath
1539 STACK_CHECK(L2)
1540 if( pcpath)
1541 {
1542 if (lua_type(L,pcpath) != LUA_TSTRING)
1543 luaL_error( L, "expected packagecpath as string, got %s", luaG_typename(L,pcpath));
1544 lua_getglobal( L2, "package");
1545 if( lua_isnil( L2, -1))
1546 {
1547 lua_pop( L2, 1);
1548 luaL_error( L, "specifying a new cpath for packages, but lane doesn't load package library");
1549 }
1550 lua_pushvalue( L, pcpath);
1551 luaG_inter_move( L, L2, 1); // moves the new cpath to L2
1552 lua_setfield( L2, -2, "cpath"); // set package.cpath
1553 lua_pop( L2, 1);
1554 }
1555 STACK_END(L2,0)
1556
1427 // Lane main function 1557 // Lane main function
1428 // 1558 //
1429 STACK_CHECK(L) 1559 STACK_CHECK(L)