Fix for PR1654 - implement "movstrsi" pattern to copy simple blocks of memory.
[official-gcc.git] / libchill / sendbuffer.c
blob4b6d97c603b8c83022f81239b92bcee0f9799b37
1 /* Implement tasking-related runtime actions for CHILL.
2 Copyright (C) 1992,1993 Free Software Foundation, Inc.
3 Author: Wilfried Moser
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* As a special exception, if you link this library with other files,
22 some of which are compiled with GCC, to produce an executable,
23 this library does not by itself cause the resulting executable
24 to be covered by the GNU General Public License.
25 This exception does not however invalidate any other reasons why
26 the executable file might be covered by the GNU General Public License. */
28 #include "rtltypes.h"
29 #include "rts.h"
31 EXCEPTION (sendfail);
33 extern void __cause_ex1 (char *ex, char *file, int lineno);
35 #define CAUSE_SENDFAIL __cause_ex1 ("sendfail", filename, lineno)
38 * function __send_buffer
40 * parameters:
41 * buffer pointer to buffer descriptor
42 * data pointer to data descriptor
43 * prio priority for send action
44 * timeout pointer to timeout value
45 * filename source file name where function gets called
46 * lineno linenumber in source file
48 * returns:
49 * int 0 .. success
50 * 1 .. timeout
52 * exceptions:
53 * sendfail
55 * abstract:
56 * implement the CHILL SEND buffer action.
59 int
60 __send_buffer (buffer, data, prio, timeout, filename, lineno)
61 Buffer_Descr *buffer;
62 Data_Descr *data;
63 int prio;
64 void *timeout;
65 char *filename;
66 int lineno;
68 Buffer_Queue *bq;
69 Buffer_Send_Queue *bsq, *bsq_entry, *prev_bsq_entry;
70 int cnt = 0;
71 int retval = 0;
73 /* if we don't have anything queued on that buffer,
74 set up the structure */
75 memcpy (&bq, buffer->buf, sizeof (Buffer_Queue *));
76 if (bq == 0)
78 MALLOC (bq, sizeof (Buffer_Queue));
79 memset (bq, 0, sizeof (Buffer_Queue));
80 memcpy (buffer->buf, &bq, sizeof (Buffer_Queue *));
83 /* look if there is a process delayed on that buffer */
84 if (bq->waitqueue != 0)
86 Buffer_Wait_Queue *listentry;
88 /* there is already a processes waiting for that buffer,
89 check datalength and copy the data in */
90 if (bq->waitqueue->datalen < data->length)
91 CAUSE_SENDFAIL;
92 memcpy (bq->waitqueue->dataptr, data->ptr, data->length);
94 /* set up the entry */
95 bq->waitqueue->is_sent = 1;
96 bq->waitqueue->who_sent = THIS;
98 /* continue waiting process */
99 __continue_that (bq->waitqueue->this, prio, filename, lineno);
101 /* now dequeue all entries of this list */
102 listentry = bq->waitqueue->startlist;
103 while (listentry != 0)
105 Buffer_Wait_Queue *tmp, *prev_entry, *bwq;
106 Buffer_Queue *bq;
108 tmp = listentry->chain;
109 memcpy (&bq, listentry->bufferaddr, sizeof (Buffer_Queue *));
110 prev_entry = (Buffer_Wait_Queue *)&bq->waitqueue;
111 bwq = bq->waitqueue;
113 while (bwq != listentry)
115 prev_entry = bwq;
116 bwq = bwq->forward;
118 /* dequeue it */
119 prev_entry->forward = bwq->forward;
120 bq->waitqueuelength--;
121 listentry = tmp;
124 /* all done */
125 return 0;
128 /* nothing in waitqueue, set up an entry for sendqueue.
129 Note: we allocate here space for the data too, to reduce
130 calls to malloc and let the dataptr point just behind
131 the Buffer_Send_Queue structure. */
132 MALLOC (bsq_entry, sizeof (Buffer_Send_Queue) + data->length);
133 memset (bsq_entry, 0, sizeof (Buffer_Send_Queue));
135 bsq_entry->priority = prio;
136 bsq_entry->this = THIS;
137 bsq_entry->datalen = data->length;
138 bsq_entry->dataptr = bsq_entry + 1;
139 memcpy (bsq_entry->dataptr, data->ptr, data->length);
141 /* add entry to sendqueue */
142 prev_bsq_entry = (Buffer_Send_Queue *)&bq->sendqueue;
143 bsq = bq->sendqueue;
145 while (bsq != 0 && bsq->priority >= prio)
147 prev_bsq_entry = bsq;
148 bsq = bsq->forward;
150 if (bsq == 0)
152 /* beginning or end of the list */
153 prev_bsq_entry->forward = bsq_entry;
155 else
157 /* somewhere in the middle */
158 bsq_entry->forward = prev_bsq_entry->forward;
159 prev_bsq_entry->forward = bsq_entry;
162 if (buffer->maxqueuelength != (unsigned long)-1L &&
163 bq->sendqueuelength >= buffer->maxqueuelength)
165 /* we have to delay this process */
166 bsq_entry->is_delayed = 1;
167 retval = __delay_this (wait_buffer_send, timeout, filename, lineno);
168 if (retval)
170 prev_bsq_entry->forward = bsq_entry->forward;
171 FREE (bsq_entry);
174 else
175 /* just say that there is one more entry in the queue */
176 bq->sendqueuelength++;
177 return retval;
180 /* force function __print_buffer to be linked */
181 extern void __print_buffer ();
182 static EntryPoint pev = __print_buffer;