Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4973 | right-hear | 1 | #include"ipc.h" |
2 | #include |
||
3 | #include |
||
4 | |||
5 | inline int ipc_messages_avail(ipc_hdr_t * hdr) |
||
6 | { |
||
7 | return hdr->free_ptr!=8; |
||
8 | } |
||
9 | |||
10 | static inline void lock_msg_queue(ipc_hdr_t * hdr) |
||
11 | { |
||
12 | int d0; |
||
13 | __asm__ __volatile__( |
||
14 | "2:\t" |
||
15 | "movb $1,%%al\n\t" |
||
16 | "xchgb %%al,%0\n\t" |
||
17 | "andb %%al,%%al\n\t" |
||
18 | "jnz 2b\n\t" |
||
19 | "incb %0" |
||
20 | :"=m"(hdr->lock),"=a"(d0) |
||
21 | :"m"(hdr->lock) |
||
22 | :"memory"); |
||
23 | } |
||
24 | |||
25 | static inline void unlock_msg_queue(ipc_hdr_t * hdr) |
||
26 | { |
||
27 | __asm__ __volatile__( |
||
28 | "movl $0,%0" |
||
29 | :"=m"(hdr->lock) |
||
30 | :"m"(hdr->lock) |
||
31 | :"memory"); |
||
32 | } |
||
33 | |||
34 | ipc_msg_t * ipc_receive_msg(ipc_hdr_t * hdr) |
||
35 | { |
||
36 | ipc_msg_t * msg, * tmp; |
||
37 | lock_msg_queue(hdr); |
||
38 | if(!ipc_messages_avail(hdr)) |
||
39 | { |
||
40 | unlock_msg_queue(hdr); |
||
41 | return NULL; |
||
42 | } |
||
43 | tmp=(ipc_msg_t *)hdr->__mem; |
||
44 | msg=(ipc_msg_t *)malloc(tmp->msg_length); |
||
45 | if(!msg) |
||
46 | { |
||
47 | unlock_msg_queue(hdr); |
||
48 | return NULL; |
||
49 | } |
||
50 | memcpy(msg,tmp,tmp->msg_length); |
||
51 | if(hdr->free_ptr>(8+tmp->msg_length)) |
||
52 | { |
||
53 | memcpy(tmp,tmp+1,hdr->free_ptr-8-tmp->msg_length); |
||
54 | hdr->free_ptr-=tmp->msg_length; |
||
55 | } |
||
56 | unlock_msg_queue(hdr); |
||
57 | return msg; |
||
58 | } |