1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
|
/*
* Copyright (c) 1996, 1998, 1999 University of Utah and the Flux Group.
* All rights reserved.
*
* This file is part of the Flux OSKit. The OSKit is free software, also known
* as "open source;" you can redistribute it and/or modify it under the terms
* of the GNU General Public License (GPL), version 2, as published by the Free
* Software Foundation (FSF). To explore alternate licensing terms, contact
* the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
*
* The OSKit is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GPL for more details. You should have
* received a copy of the GPL along with the OSKit; see the file COPYING. If
* not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
*/
/*
* Simple IPC.
*/
#include <threads/pthread_internal.h>
#include <threads/pthread_ipc.h>
#include <stdlib.h>
#define MIN(x, y) ((x) <= (y) ? (x) : (y))
#define ANYTID ((pthread_t) -1)
#define NOTID ((pthread_t) 0)
#ifdef MEASURE
#include <oskit/machine/proc_reg.h>
struct ipc_stats {
int sends;
int send_cycles;
int sblocks;
int sblock_cycles;
int recvs;
int recv_cycles;
int rblocks;
int rblock_cycles;
int waits;
int wait_cycles;
int wfails;
int wfail_cycles;
int wblocks;
int wblock_cycles;
int waitfails;
int waitfail_cycles;
int scheds;
int sched_cycles;
int schednrs;
int schednr_cycles;
int schedws;
int schedw_cycles;
};
static struct ipc_stats stats;
void dump_ipc_stats();
#endif
void
pthread_init_ipc(void)
{
#ifdef MEASURE
atexit(dump_ipc_stats);
#endif
}
/*
* Send a message. Synchronous. Caller blocks until receiver picks
* up the message, or until the timeout expires.
*/
oskit_error_t
oskit_ipc_send(pthread_t dst,
void *msg, oskit_size_t msg_size, oskit_s32_t timeout)
{
pthread_thread_t *pthread = CURPTHREAD();
pthread_thread_t *target;
int err = 0;
#ifdef MEASURE
unsigned long before, after;
before = get_tsc();
#endif
pthread_testcancel();
if ((target = tidtothread(dst)) == NULL_THREADPTR)
return EINVAL;
/*
* To avoid deadlock, everyone locks the recv side first.
*/
assert_interrupts_enabled();
disable_interrupts();
pthread_lock(&target->waitlock);
/*
* Check the target thread. If its already waiting for a message
* from *this* thread, go ahead and deliver the message.
*/
if (target->waitflags & THREAD_WS_IPCRECV_WAIT &&
(target->ipc_state.tid == pthread->tid ||
target->ipc_state.tid == ANYTID)) {
if (msg_size) {
memcpy(target->ipc_state.msg,
msg, MIN(msg_size, target->ipc_state.msg_size));
if (msg_size > target->ipc_state.msg_size)
err = OSKIT_ERANGE;
}
/*
* Tell the receiver the amount of data sent, and who sent
* it in case the receiver was in a wait instead of a recv.
*/
target->ipc_state.msg_size = msg_size;
target->ipc_state.tid = pthread->tid;
/*
* Clear the recv wait.
*/
target->waitflags &= ~THREAD_WS_IPCRECV_WAIT;
pthread_unlock(&target->waitlock);
#ifdef MEASURE
after = get_tsc();
if (after > before) {
stats.sends++;
stats.send_cycles += (after - before);
}
#endif
/*
* And handoff to the receiver. No waitstate is provided,
* so the sender yields to the receiver (sender does not
* block).
*/
pthread_sched_handoff(0, target);
enable_interrupts();
return err;
}
pthread_lock(&pthread->waitlock);
/*
* Add to receiver's senders queue. Necessary if the receiver does
* a wait instead of a recv.
*/
queue_enter(&target->ipc_state.senders,
pthread, pthread_thread_t *, ipc_state.senders_chain)
pthread->ipc_state.msg = msg;
pthread->ipc_state.msg_size = msg_size;
pthread->ipc_state.tid = dst;
pthread->waitflags |= THREAD_WS_IPCSEND_WAIT;
pthread_unlock(&target->waitlock);
#ifdef MEASURE
after = get_tsc();
if (after > before) {
stats.sblocks++;
stats.sblock_cycles += (after - before);
}
#endif
pthread_sched_reschedule(RESCHED_BLOCK, &pthread->waitlock);
/*
* Back from send. The receiver woke us up, or we got canceled.
*/
pthread_lock(&pthread->lock);
if (pthread->flags & THREAD_CANCELED) {
pthread_unlock(&pthread->lock);
enable_interrupts();
return OSKIT_ECANCELED;
}
pthread_unlock(&pthread->lock);
/*
* Message was transfered. Check for short message.
*/
if (msg_size > pthread->ipc_state.msg_size)
err = OSKIT_ERANGE;
enable_interrupts();
return err;
}
/*
* Blocking receive. The receiver waits for a message from the specified
* source thread.
*/
oskit_error_t
oskit_ipc_recv(pthread_t src,
void *msg, oskit_size_t msg_size, oskit_size_t *actual,
oskit_s32_t timeout)
{
pthread_thread_t *pthread = CURPTHREAD();
pthread_thread_t *source;
int err = 0;
#ifdef MEASURE
unsigned long before, after;
before = get_tsc();
#endif
pthread_testcancel();
if ((source = tidtothread(src)) == NULL_THREADPTR)
return EINVAL;
/*
* To avoid deadlock, everyone locks the recv side first.
*/
assert_interrupts_enabled();
disable_interrupts();
pthread_lock(&pthread->waitlock);
/*
* Look to see if the source thread is in the send side.
* If so, the message can be transfered from the sender
* and the sender woken up.
*/
pthread_lock(&source->waitlock);
if (source->waitflags & THREAD_WS_IPCSEND_WAIT &&
source->ipc_state.tid == pthread->tid) {
*actual = MIN(msg_size, source->ipc_state.msg_size);
if (*actual) {
memcpy(msg, source->ipc_state.msg, *actual);
if (source->ipc_state.msg_size > msg_size)
err = OSKIT_ERANGE;
}
/*
* Since the sender was waiting, it was on the receiver's
* senders queue. Remove it.
*/
queue_remove(&pthread->ipc_state.senders, source,
pthread_thread_t *, ipc_state.senders_chain);
/*
* Tell the sender the amount of data received.
*/
source->ipc_state.msg_size = *actual;
/*
* Clear the send wait. If the the sender side initiated
* an RPC, do not wake it up yet. The reply will do it.
*/
source->waitflags &= ~THREAD_WS_IPCSEND_WAIT;
if (! (source->waitflags & THREAD_WS_IPCREPL_WAIT)) {
pthread_unlock(&source->waitlock);
pthread_sched_setrunnable(source);
}
else
pthread_unlock(&source->waitlock);
pthread_unlock(&pthread->waitlock);
#ifdef MEASURE
after = get_tsc();
if (after > before) {
stats.recvs++;
stats.recv_cycles += (after - before);
}
#endif
enable_interrupts();
return err;
}
pthread_unlock(&source->waitlock);
/*
* No sender is waiting. Set up the receiver to wait for
* a sender.
*/
pthread->ipc_state.msg = msg;
pthread->ipc_state.msg_size = msg_size;
pthread->ipc_state.tid = src;
pthread->waitflags |= THREAD_WS_IPCRECV_WAIT;
#ifdef MEASURE
after = get_tsc();
if (after > before) {
stats.rblocks++;
stats.rblock_cycles += (after - before);
}
#endif
pthread_sched_reschedule(RESCHED_BLOCK, &pthread->waitlock);
/*
* Back from receiver. The sender woke us up, or we got canceled.
*/
pthread_lock(&pthread->lock);
if (pthread->flags & THREAD_CANCELED) {
pthread_unlock(&pthread->lock);
enable_interrupts();
return OSKIT_ECANCELED;
}
pthread_unlock(&pthread->lock);
/*
* Message was transfered. Check for short message.
*/
*actual = pthread->ipc_state.msg_size;
if (pthread->ipc_state.msg_size > msg_size)
err = OSKIT_ERANGE;
enable_interrupts();
return err;
}
/*
* Blocking wait. The receiver waits for a message from any source thread.
* The id of the source is returned.
*/
oskit_error_t
oskit_ipc_wait(pthread_t *src,
void *msg, oskit_size_t msg_size, oskit_size_t *actual,
oskit_s32_t timeout)
{
pthread_thread_t *pthread = CURPTHREAD();
pthread_thread_t *source;
int err = 0;
#ifdef MEASURE
unsigned long before, after;
before = get_tsc();
#endif
pthread_testcancel();
/*
* To avoid deadlock, everyone locks the recv side first.
*/
assert_interrupts_enabled();
disable_interrupts();
pthread_lock(&pthread->waitlock);
/*
* Since this is non-specific receive, it is up to the senders
* to initiate. Look for a waiting sender in the senders queue
* first.
*/
if (queue_empty(&pthread->ipc_state.senders))
goto wait;
/*
* A sender is waiting.
*/
queue_remove_first(&pthread->ipc_state.senders, source,
pthread_thread_t *, ipc_state.senders_chain);
pthread_lock(&source->waitlock);
assert(source->waitflags & THREAD_WS_IPCSEND_WAIT &&
source->ipc_state.tid == pthread->tid);
/*
* The message can be transfered from the sender, and then the
* sender is woken up.
*/
*actual = MIN(msg_size, source->ipc_state.msg_size);
if (*actual) {
memcpy(msg, source->ipc_state.msg, *actual);
if (source->ipc_state.msg_size > msg_size)
err = OSKIT_ERANGE;
}
/*
* Tell the sender the amount of data received.
*/
source->ipc_state.msg_size = *actual;
/*
* Tell the caller who sent the message.
*/
*src = source->tid;
/*
* Clear the send wait. If the the sender side initiated
* an RPC, do not wake it up yet. The reply will do it.
*/
source->waitflags &= ~THREAD_WS_IPCSEND_WAIT;
if (! (source->waitflags & THREAD_WS_IPCREPL_WAIT)) {
pthread_unlock(&source->waitlock);
pthread_sched_setrunnable(source);
}
else
pthread_unlock(&source->waitlock);
pthread_unlock(&pthread->waitlock);
#ifdef MEASURE
after = get_tsc();
if (after > before) {
stats.waits++;
stats.wait_cycles += (after - before);
}
#endif
enable_interrupts();
return err;
wait:
/*
* Only two timeout values right now, 0 and forever ...
*/
if (timeout == 0) {
pthread_unlock(&pthread->waitlock);
#ifdef MEASURE
after = get_tsc();
if (after > before) {
stats.wfails++;
stats.wfail_cycles += (after - before);
}
#endif
enable_interrupts();
return OSKIT_EAGAIN;
}
/*
* The receiver must wait ...
*/
pthread->ipc_state.msg = msg;
pthread->ipc_state.msg_size = msg_size;
pthread->ipc_state.tid = ANYTID;
pthread->waitflags |= THREAD_WS_IPCRECV_WAIT;
#ifdef MEASURE
after = get_tsc();
if (after > before) {
stats.wblocks++;
stats.wblock_cycles += (after - before);
}
#endif
pthread_sched_reschedule(RESCHED_BLOCK, &pthread->waitlock);
/*
* Back from receiver. The sender woke us up, or we got canceled.
*/
pthread_lock(&pthread->lock);
if (pthread->flags & THREAD_CANCELED) {
pthread_unlock(&pthread->lock);
enable_interrupts();
return OSKIT_ECANCELED;
}
pthread_unlock(&pthread->lock);
/*
* Message was transfered. Check for short message.
*/
*src = pthread->ipc_state.tid;
*actual = pthread->ipc_state.msg_size;
if (pthread->ipc_state.msg_size > msg_size)
err = OSKIT_ERANGE;
enable_interrupts();
return err;
}
/*
* Send a message. Synchronous. Caller blocks until receiver picks
* up the message, or until the timeout expires.
*/
oskit_error_t
oskit_ipc_call(pthread_t dst,
void *sendmsg, oskit_size_t sendmsg_size,
void *recvmsg, oskit_size_t recvmsg_size, oskit_size_t *actual,
oskit_s32_t timeout)
{
pthread_thread_t *pthread = CURPTHREAD();
pthread_thread_t *target;
int err = 0;
pthread_testcancel();
if ((target = tidtothread(dst)) == NULL_THREADPTR)
return EINVAL;
/*
* To avoid deadlock, everyone locks the recv side first.
*/
assert_interrupts_enabled();
disable_interrupts();
pthread_lock(&target->waitlock);
/*
* Check the target thread. If its already waiting for a message
* from *this* thread, go ahead and deliver the message.
*/
if (target->waitflags & THREAD_WS_IPCRECV_WAIT &&
(target->ipc_state.tid == pthread->tid ||
target->ipc_state.tid == ANYTID)) {
if (sendmsg_size) {
memcpy(target->ipc_state.msg, sendmsg,
MIN(sendmsg_size, target->ipc_state.msg_size));
if (sendmsg_size > target->ipc_state.msg_size)
err = OSKIT_ERANGE;
}
/*
* Tell the receiver the amount of data sent, and who sent
* it in case the receiver was in a wait instead of a recv.
*/
target->ipc_state.msg_size = sendmsg_size;
target->ipc_state.tid = pthread->tid;
/*
* Setup sender to recv the reply.
*/
pthread->ipc_state.reply = recvmsg;
pthread->ipc_state.reply_size = recvmsg_size;
pthread->ipc_state.tid = dst;
/*
* Clear the recv wait.
*/
target->waitflags &= ~THREAD_WS_IPCRECV_WAIT;
pthread_unlock(&target->waitlock);
/*
* And handoff to the receiver, waiting for reply message.
*/
pthread_sched_handoff(THREAD_WS_IPCREPL_WAIT, target);
goto reply;
}
pthread_lock(&pthread->waitlock);
/*
* Add to receiver's senders queue. Necessary if the receiver does
* a wait instead of a recv.
*/
queue_enter(&target->ipc_state.senders,
pthread, pthread_thread_t *, ipc_state.senders_chain);
pthread->ipc_state.msg = sendmsg;
pthread->ipc_state.msg_size = sendmsg_size;
pthread->ipc_state.reply = recvmsg;
pthread->ipc_state.reply_size = recvmsg_size;
pthread->ipc_state.tid = dst;
pthread->waitflags |= (THREAD_WS_IPCSEND_WAIT|
THREAD_WS_IPCREPL_WAIT);
pthread_unlock(&target->waitlock);
pthread_sched_reschedule(RESCHED_BLOCK, &pthread->waitlock);
/*
* Back from call. The receiver woke us up with the reply, or we
* got canceled.
*/
reply:
pthread_lock(&pthread->lock);
if (pthread->flags & THREAD_CANCELED) {
pthread_unlock(&pthread->lock);
enable_interrupts();
return OSKIT_ECANCELED;
}
pthread_unlock(&pthread->lock);
/*
* Message was transfered. Check for short message.
*/
*actual = pthread->ipc_state.reply_size;
if (pthread->ipc_state.reply_size > recvmsg_size)
err = OSKIT_ERANGE;
enable_interrupts();
return err;
}
/*
* Reply to an RPC. The target thread must be in the RPC client side
* wait, or the call fails.
*/
oskit_error_t
oskit_ipc_reply(pthread_t src, void *msg, oskit_size_t msg_size)
{
pthread_thread_t *pthread = CURPTHREAD();
pthread_thread_t *source;
int err = 0;
if ((source = tidtothread(src)) == NULL_THREADPTR)
return OSKIT_EINVAL;
/*
* To avoid deadlock, everyone locks the recv side first.
*/
assert_interrupts_enabled();
disable_interrupts();
pthread_lock(&pthread->waitlock);
/*
* Check the sender to make sure its in a RPC reply wait from this
* thread. Error if not.
*/
pthread_lock(&source->waitlock);
if (! (source->waitflags & THREAD_WS_IPCREPL_WAIT &&
source->ipc_state.tid == pthread->tid)) {
pthread_unlock(&source->waitlock);
pthread_unlock(&pthread->waitlock);
enable_interrupts();
return OSKIT_EINVAL;
}
/*
* Copy back the reply and look for a short message.
*/
if (msg_size) {
memcpy(source->ipc_state.reply,
msg, MIN(msg_size, source->ipc_state.reply_size));
if (msg_size > source->ipc_state.reply_size)
err = OSKIT_ERANGE;
}
/*
* Tell the sender the size of the reply.
*/
source->ipc_state.reply_size = msg_size;
/*
* Clear the reply wait.
*/
source->waitflags &= ~THREAD_WS_IPCREPL_WAIT;
pthread_unlock(&source->waitlock);
pthread_unlock(&pthread->waitlock);
/*
* And handoff back to the sender. No waitstate is provided,
* so we yield to the sender (we do not block).
*/
pthread_sched_handoff(0, source);
enable_interrupts();
return err;
}
/*
* Cancelation Support.
*/
void
pthread_ipc_cancel(pthread_thread_t *pthread)
{
if (pthread->waitflags & THREAD_WS_IPCRECV_WAIT) {
pthread->waitflags &= ~THREAD_WS_IPCRECV_WAIT;
pthread_unlock(&pthread->waitlock);
pthread_sched_setrunnable(pthread);
}
else
panic("oskit_ipc_cancel");
}
#ifdef MEASURE
void
dump_ipc_stats()
{
printf("IPC Stats:\n");
printf("Sends: %d\n", stats.sends);
printf("Send Cycles: %u\n", stats.send_cycles);
printf("Send Blocks: %u\n", stats.sblocks);
printf("Send Block Cyc: %u\n", stats.sblock_cycles);
printf("Recvs: %d\n", stats.recvs);
printf("Recv Cycles: %u\n", stats.recv_cycles);
printf("Recv Blocks: %u\n", stats.rblocks);
printf("Recv Block Cyc: %u\n", stats.rblock_cycles);
printf("Waits: %d\n", stats.waits);
printf("Wait Cycles: %u\n", stats.wait_cycles);
printf("Wait Fails: %d\n", stats.wfails);
printf("Wait Fail Cyc: %u\n", stats.wfail_cycles);
printf("Wait Blocks: %d\n", stats.wblocks);
printf("Wait Block Cyc: %u\n", stats.wblock_cycles);
printf("Sched Msgs: %d\n", stats.scheds);
printf("Sched Msg Cyc: %u\n", stats.sched_cycles);
printf("Sched NotR: %d\n", stats.schednrs);
printf("Sched NotR Cyc: %u\n", stats.schednr_cycles);
printf("Sched Waits: %d\n", stats.schedws);
printf("Sched Wait Cyc: %u\n", stats.schedw_cycles);
}
#endif
|