comparison Renderer/Engine/lindaapi.cc @ 606:32a7260fad2f

32bit/64bit ABI (64 bit is not tested yet). 64bit ABI requires 64bit SDL library etc.
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 07 Nov 2009 18:05:52 +0900
parents 90c61fe2b109
children 0decff4e867b
comparison
equal deleted inserted replaced
605:90c61fe2b109 606:32a7260fad2f
18 #include <sys/un.h> 18 #include <sys/un.h>
19 #include <errno.h> 19 #include <errno.h>
20 #include <arpa/inet.h> 20 #include <arpa/inet.h>
21 21
22 #include "lindaapi.h" 22 #include "lindaapi.h"
23 #if 0 23
24 24
25 #if 0 25 #if 0
26 #define PSX_Debug(deb) (putchar(PS_DEB)),\ 26 #define PSX_Debug(deb) (putchar(PS_DEB)),\
27 (printf deb ),\ 27 (printf deb ),\
28 (putchar(PS_DEB)) 28 (putchar(PS_DEB))
40 static int g_max_fds = 0; /* 監視するFDの最大値 */ 40 static int g_max_fds = 0; /* 監視するFDの最大値 */
41 41
42 /* Static Functions */ 42 /* Static Functions */
43 static void unix_chkserv(int ps); 43 static void unix_chkserv(int ps);
44 void psx_free(void *); 44 void psx_free(void *);
45 static int psx_queue(unsigned int tspace_id, unsigned int id, 45 static long psx_queue(unsigned int tspace_id, unsigned int id,
46 unsigned int size, unsigned char *data, char mode, 46 unsigned int size, unsigned char *data, char mode,
47 void(*callback)(unsigned char *,void *),void * obj); 47 void(*callback)(unsigned char *,void *),void * obj);
48 48
49 #ifdef COUNT_PACKET 49 #ifdef COUNT_PACKET
50 // print packet count message per PRINT_INTERVAL sec 50 // print packet count message per PRINT_INTERVAL sec
312 data - 送信するデータ 312 data - 送信するデータ
313 size - dataのサイズ 313 size - dataのサイズ
314 返り値: 314 返り値:
315 シーケンス番号 315 シーケンス番号
316 /-------------------------------------------------------------------*/ 316 /-------------------------------------------------------------------*/
317 int 317 long
318 psx_out(unsigned int tspace_id, unsigned int id, 318 psx_out(unsigned int tspace_id, unsigned int id,
319 unsigned char *data, unsigned int size){ 319 unsigned char *data, unsigned int size){
320 int r; 320 long r;
321 if ((r = psx_queue(tspace_id, id, size, data, 'o', NULL, NULL)) == FAIL) { 321 if ((r = psx_queue(tspace_id, id, size, data, 'o', NULL, NULL)) == FAIL) {
322 return(FAIL); 322 return(FAIL);
323 } 323 }
324 DEB( fprintf(stdout, "psx_out: size = %d, command = %s\n", 324 DEB( fprintf(stdout, "psx_out: size = %d, command = %s\n",
325 q_end->size, q_end->command+LINDA_HEADER_SIZE)); 325 q_end->size, q_end->command+LINDA_HEADER_SIZE));
341 使用しない場合はNULLをいれる。 341 使用しない場合はNULLをいれる。
342 obj - コールバックで用いる関数の引き数。 342 obj - コールバックで用いる関数の引き数。
343 返り値: 343 返り値:
344 psx_queue内でmallocされたREPLY構造体へのポインタ 344 psx_queue内でmallocされたREPLY構造体へのポインタ
345 /-------------------------------------------------------------------*/ 345 /-------------------------------------------------------------------*/
346 int 346 long
347 psx_ld(unsigned int tspace_id, unsigned int id, 347 psx_ld(unsigned int tspace_id, unsigned int id,
348 char mode, void(*callback)(unsigned char *,void *), void * obj){ 348 char mode, void(*callback)(unsigned char *,void *), void * obj){
349 int r; 349 long r;
350 if ((r = psx_queue(tspace_id, id, 0, NULL, mode, callback, obj)) == FAIL) { 350 if ((r = psx_queue(tspace_id, id, 0, NULL, mode, callback, obj)) == FAIL) {
351 return(FAIL); 351 return(FAIL);
352 } 352 }
353 return(r); 353 return(r);
354 } 354 }
369 REPLY *p, *q; 369 REPLY *p, *q;
370 unsigned char *ans; 370 unsigned char *ans;
371 371
372 DEB(fprintf(stdout, "psx_reply: search of seq = %d\n", seq)); 372 DEB(fprintf(stdout, "psx_reply: search of seq = %d\n", seq));
373 PSX_Debug(("psx_reply: seq %d", seq)); 373 PSX_Debug(("psx_reply: seq %d", seq));
374 // linear search なの?
375 for(q = NULL,p = reply; p; q = p,p = p->next){ 374 for(q = NULL,p = reply; p; q = p,p = p->next){
376 if (p->seq == seq){ 375 if (p->seq == seq){
377 DEB(fprintf(stdout, "psx_reply: match of seq = %d\n", seq)); 376 DEB(fprintf(stdout, "psx_reply: match of seq = %d\n", seq));
378 if (p->mode == '!'){ 377 if (p->mode == '!'){
379 ans = p->answer; 378 ans = p->answer;
448 } 447 }
449 } 448 }
450 } 449 }
451 450
452 /*-------------------------------------------------------------------/ 451 /*-------------------------------------------------------------------/
453 static long 452 static int
454 psx_queue (unsigned int tspace_id, unsigned int id, 453 psx_queue (unsigned int tspace_id, unsigned int id,
455 unsigned int size, unsigned char *data, char mode, 454 unsigned int size, unsigned char *data, char mode,
456 void(*callback)(char*,void*), void * obj): 455 void(*callback)(char*,void*), void * obj):
457 out,in,read,waitなどのコマンドをCOMMANDキューに溜める。データを 456 out,in,read,waitなどのコマンドをCOMMANDキューに溜める。データを
458 受信するコマンド(in,read,wait)のときは受け取ったときにデータを 457 受信するコマンド(in,read,wait)のときは受け取ったときにデータを
470 返り値: 469 返り値:
471 成功した場合 - mallocしたREPLY構造体へのポインタ。outの場合は 470 成功した場合 - mallocしたREPLY構造体へのポインタ。outの場合は
472 0が返る。 471 0が返る。
473 失敗した場合 - FAIL(-1)が返る。 472 失敗した場合 - FAIL(-1)が返る。
474 /-------------------------------------------------------------------*/ 473 /-------------------------------------------------------------------*/
475 static int 474 static long
476 psx_queue(unsigned int tspace_id, unsigned int id, 475 psx_queue(unsigned int tspace_id, unsigned int id,
477 unsigned int size, unsigned char *data, char mode, 476 unsigned int size, unsigned char *data, char mode,
478 void(*callback)(unsigned char *,void *), void * obj){ 477 void(*callback)(unsigned char *,void *), void * obj){
479 REPLY *p; 478 REPLY *p;
480 COMMAND *c; 479 COMMAND *c;
505 } 504 }
506 505
507 /* データ受け取り要求(in,rd,wait)なら受け取り用の箱を用意 */ 506 /* データ受け取り要求(in,rd,wait)なら受け取り用の箱を用意 */
508 if (mode != 'o') { 507 if (mode != 'o') {
509 if (reply == NULL){ 508 if (reply == NULL){
510 reply = new_reply(); 509 if ((reply = (REPLY *) malloc (sizeof(REPLY))) == NULL){
510 return(FAIL);
511 }
511 p = r_end = reply; p->next = NULL; 512 p = r_end = reply; p->next = NULL;
512 } else { 513 } else {
513 r_end->next = new_reply(); 514 if ((r_end->next = (REPLY *) malloc (sizeof(REPLY))) == NULL){
515 return(FAIL);
516 }
514 p = r_end->next; r_end = p; p->next = NULL; 517 p = r_end->next; r_end = p; p->next = NULL;
515 } 518 }
516 p->mode = '?'; 519 p->mode = '?';
517 p->seq = p->reply_id; 520 p->seq = (long)p; // 構造体のアドレスで識別
518 p->callback = callback; 521 p->callback = callback;
519 p->obj = obj; 522 p->obj = obj;
520 PSX_Debug(("psx_queue: seq %d reply %x p %x r_end %x",seq,reply,p,r_end)); 523 PSX_Debug(("psx_queue: seq %d reply %x p %x r_end %x",seq,reply,p,r_end));
521 } else { 524 } else {
522 p = 0; 525 p = 0;
528 531
529 q_end->command[LINDA_SEQ_OFFSET] = ((long)p>>24) & 0xff; 532 q_end->command[LINDA_SEQ_OFFSET] = ((long)p>>24) & 0xff;
530 q_end->command[LINDA_SEQ_OFFSET+1] = ((long)p>>16) & 0xff; 533 q_end->command[LINDA_SEQ_OFFSET+1] = ((long)p>>16) & 0xff;
531 q_end->command[LINDA_SEQ_OFFSET+2] = ((long)p>>8) & 0xff; 534 q_end->command[LINDA_SEQ_OFFSET+2] = ((long)p>>8) & 0xff;
532 q_end->command[LINDA_SEQ_OFFSET+3] = ((long)p) & 0xff; 535 q_end->command[LINDA_SEQ_OFFSET+3] = ((long)p) & 0xff;
533 536
537 q_end->command[LINDA_DATA_LENGTH_OFFSET] = (size>>24) & 0xff;
538 q_end->command[LINDA_DATA_LENGTH_OFFSET+1] = (size>>16) & 0xff;
539 q_end->command[LINDA_DATA_LENGTH_OFFSET+2] = (size>>8) & 0xff;
540 q_end->command[LINDA_DATA_LENGTH_OFFSET+3] = (size) & 0xff;
534 541
535 q_end->size = size+LINDA_HEADER_SIZE; /* command size */ 542 q_end->size = size+LINDA_HEADER_SIZE; /* command size */
536 q_end->tspace_id = tspace_id; /* destination id */ 543 q_end->tspace_id = tspace_id; /* destination id */
537 q_end->next = NULL; 544 q_end->next = NULL;
538 qsize++; 545 qsize++;
711 tuple[LINDA_MODE_OFFSET] = mode; 718 tuple[LINDA_MODE_OFFSET] = mode;
712 } 719 }
713 720
714 721
715 722
716
717 static ReplyPtr freeReply; /* Free Pool of Reply */
718 static ReplyPtr replyPool; /* List of malloced free Reply */
719
720 static ReplyPtr extend_reply_pool(int num);
721
722 static const int QUEUE_MALLOC_ERROR = 10;
723 static const int POOL_SIZE_OVER_ERROR = 11;
724
725 static int reply_errno;
726
727 //
728 // Initialize Reply Pool
729 // return NULL on error
730 //
731
732 static ReplyPtr
733 init_reply(int num)
734 {
735 ReplyPtr q;
736 if (! replyPool) {
737 return extend_reply_pool(num);
738 }
739 return replyPool;
740 }
741
742 static ReplyPtr
743 extend_reply_pool(int num)
744 {
745 ReplyPtr q;
746 // Keep malloc history in ReplyPool
747 q = (ReplyPtr) malloc(sizeof(Reply) * (num+1));
748 if (!q) {
749 reply_errno = QUEUE_MALLOC_ERROR;
750 retrun q;
751 }
752 q->next = replyPool;
753 replyPool = q;
754 // Connect all free reply in the pool
755 q = queeuePool + 1;
756 for(q = replyPool+1; num-->0; q++) q->next = q+1;
757 q->next = freeReply;
758 freeReply = replyPool+1;
759 return replyPool;
760 }
761
762 static void
763 destory_reply()
764 {
765 ReplyPtr q;
766 for(q = replyPool ;q; q = q->next) {
767 free(q);
768 }
769 freeReply = replyPool = NULL;
770 }
771
772 static ReplyPtr
773 new_reply(int priority, int task)
774 {
775 ReplyPtr q;
776 if (!freeReply) {
777 pool_size *= 2;
778 if (pool_size > MAX_POOL_SIZE) {
779 reply_errno = POOL_SIZE_OVER_ERROR;
780 return NULL;
781 }
782 if (!extend_reply_pool(pool_size)) {
783 return NULL;
784 }
785 }
786 q = freeReply;
787 freeReply = freeReply->next;
788 q->next = NULL;
789 q->priority = priority;
790 q->task = task;
791 return q;
792 }
793
794 static void
795 free_reply(ReplyPtr q)
796 {
797 q->next = freeReply;
798 freeReply = q;
799 }
800
801 static TaskListPtr
802 new_task_list(char *name)
803 {
804 TaskLilstPtr task;
805 task = (TaskListPtr) malloc(sizeof(TaskList));
806 task->reply = NULL;
807 task->name = name;
808 }
809
810 static void
811 free_task_list(TaskListPtr task)
812 {
813 free(task);
814 }
815
816 static void
817 insert_reply(TaskListPtr list,ReplyPtr q)
818 {
819 q->next = list->reply;
820 list->reply = q;
821 }
822
823 static void
824 qppend_reply(TaskListPtr list,ReplyPtr q)
825 {
826 ReplyPtr p = list->reply;
827 while(p->next) p = p->next;
828 p->next = q;
829 }
830
831 static void
832 remove_reply(TaskListPtr list,ReplyPtr q)
833 {
834 ReplyPtr p = list->reply;
835 ReplyPtr p1 = p->next;
836
837 while(p1 && p1 != q) { p1 = p1->next; p = p->next; }
838 if (p1) {
839 p->next = p1->next;
840 }
841 }
842
843 #else
844
845 void
846 psx_sync_n(){
847 }
848
849 #endif
850
851 /* end */ 723 /* end */