| 1 | //
 | 
|---|
| 2 | // messshm.h
 | 
|---|
| 3 | //
 | 
|---|
| 4 | // Copyright (C) 1996 Limit Point Systems, Inc.
 | 
|---|
| 5 | //
 | 
|---|
| 6 | // Author: Curtis Janssen <cljanss@limitpt.com>
 | 
|---|
| 7 | // Maintainer: LPS
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // This file is part of the SC Toolkit.
 | 
|---|
| 10 | //
 | 
|---|
| 11 | // The SC Toolkit is free software; you can redistribute it and/or modify
 | 
|---|
| 12 | // it under the terms of the GNU Library General Public License as published by
 | 
|---|
| 13 | // the Free Software Foundation; either version 2, or (at your option)
 | 
|---|
| 14 | // any later version.
 | 
|---|
| 15 | //
 | 
|---|
| 16 | // The SC Toolkit is distributed in the hope that it will be useful,
 | 
|---|
| 17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 19 | // GNU Library General Public License for more details.
 | 
|---|
| 20 | //
 | 
|---|
| 21 | // You should have received a copy of the GNU Library General Public License
 | 
|---|
| 22 | // along with the SC Toolkit; see the file COPYING.LIB.  If not, write to
 | 
|---|
| 23 | // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 | 
|---|
| 24 | //
 | 
|---|
| 25 | // The U.S. Government is granted a limited license as per AL 91-7.
 | 
|---|
| 26 | //
 | 
|---|
| 27 | 
 | 
|---|
| 28 | #ifndef _util_group_messshm_h
 | 
|---|
| 29 | #define _util_group_messshm_h
 | 
|---|
| 30 | 
 | 
|---|
| 31 | #include <unistd.h>
 | 
|---|
| 32 | #include <sys/types.h>
 | 
|---|
| 33 | #include <sys/ipc.h>
 | 
|---|
| 34 | #include <sys/sem.h>
 | 
|---|
| 35 | #include <sys/shm.h>
 | 
|---|
| 36 | 
 | 
|---|
| 37 | #include <util/group/message.h>
 | 
|---|
| 38 | 
 | 
|---|
| 39 | namespace sc {
 | 
|---|
| 40 | 
 | 
|---|
| 41 | #define SHMCOMMBUFSIZE 1500000
 | 
|---|
| 42 | 
 | 
|---|
| 43 | /* Set the maximum number of processors (including the host). */
 | 
|---|
| 44 | #define MAXPROCS 17
 | 
|---|
| 45 | 
 | 
|---|
| 46 | 
 | 
|---|
| 47 | struct commbuf_struct {
 | 
|---|
| 48 |     int nmsg;
 | 
|---|
| 49 |     int n_wait_for_change;
 | 
|---|
| 50 |     int n_sync;
 | 
|---|
| 51 |     char buf[SHMCOMMBUFSIZE];
 | 
|---|
| 52 | };
 | 
|---|
| 53 | typedef struct commbuf_struct commbuf_t;
 | 
|---|
| 54 | 
 | 
|---|
| 55 | struct msgbuf_struct {
 | 
|---|
| 56 |     int type;
 | 
|---|
| 57 |     int from;
 | 
|---|
| 58 |     int size;
 | 
|---|
| 59 | };
 | 
|---|
| 60 | typedef struct msgbuf_struct msgbuf_t;
 | 
|---|
| 61 | 
 | 
|---|
| 62 | /** The ShmMessageGrp class is an implementation of MessageGrp that
 | 
|---|
| 63 | allows multiple process to be started that communicate with shared memory.
 | 
|---|
| 64 | This only provides improved performance if you have multiple CPU's in a
 | 
|---|
| 65 | symmetric multiprocessor configuration.  Nonetheless, it is quite useful on
 | 
|---|
| 66 | a single CPU for tracking down bugs.
 | 
|---|
| 67 | */
 | 
|---|
| 68 | class ShmMessageGrp: public intMessageGrp {
 | 
|---|
| 69 |   protected:
 | 
|---|
| 70 |     void basic_send(int target, int type, const void* data, int nbyte);
 | 
|---|
| 71 |     void basic_recv(int type, void* data, int nbyte);
 | 
|---|
| 72 |     int basic_probe(int type);
 | 
|---|
| 73 |     void initialize(int nprocs);
 | 
|---|
| 74 |     void initialize();
 | 
|---|
| 75 | 
 | 
|---|
| 76 |     // previously static variables
 | 
|---|
| 77 |     commbuf_t *commbuf[MAXPROCS];
 | 
|---|
| 78 |     int shmid;
 | 
|---|
| 79 |     int semid;
 | 
|---|
| 80 |     int change_semid;
 | 
|---|
| 81 |     void* sharedmem;
 | 
|---|
| 82 |     struct sembuf semdec;
 | 
|---|
| 83 |     struct sembuf seminc;
 | 
|---|
| 84 | 
 | 
|---|
| 85 |     // previously static functions for semephore operations
 | 
|---|
| 86 |     msgbuf_t *NEXT_MESSAGE(msgbuf_t *m);
 | 
|---|
| 87 |     void get_change(int node);
 | 
|---|
| 88 |     void put_change(int node);
 | 
|---|
| 89 |     void wait_for_write(int node);
 | 
|---|
| 90 |     void release_write(int node);
 | 
|---|
| 91 | #ifdef DEBUG
 | 
|---|
| 92 |     void print_buffer(int node, int me);
 | 
|---|
| 93 | #endif
 | 
|---|
| 94 |   public:
 | 
|---|
| 95 |     /// Reads the number of processors from environmental variable NUMPROC.
 | 
|---|
| 96 |     ShmMessageGrp();
 | 
|---|
| 97 |     /** The ShmMessageGrp KeyVal constructor takes a single keyword that
 | 
|---|
| 98 |        specifies the number of processors.  Here is an example of a
 | 
|---|
| 99 |        ParsedKeyVal input that creates a ShmMessageGrp that runs on four
 | 
|---|
| 100 |        processors:
 | 
|---|
| 101 | 
 | 
|---|
| 102 |        <pre>
 | 
|---|
| 103 |        message<ShmMessageGrp>: n = 4
 | 
|---|
| 104 |        </pre>
 | 
|---|
| 105 |     */
 | 
|---|
| 106 |     ShmMessageGrp(const Ref<KeyVal>&);
 | 
|---|
| 107 |     /// Initialize ShmMessageGrp to use nprocs processors.
 | 
|---|
| 108 |     ShmMessageGrp(int nprocs);
 | 
|---|
| 109 |     ~ShmMessageGrp();
 | 
|---|
| 110 |     void sync();
 | 
|---|
| 111 | 
 | 
|---|
| 112 |     Ref<MessageGrp> clone(void);
 | 
|---|
| 113 | };
 | 
|---|
| 114 | 
 | 
|---|
| 115 | }
 | 
|---|
| 116 |      
 | 
|---|
| 117 | #endif
 | 
|---|
| 118 | 
 | 
|---|
| 119 | // Local Variables:
 | 
|---|
| 120 | // mode: c++
 | 
|---|
| 121 | // c-file-style: "CLJ"
 | 
|---|
| 122 | // End:
 | 
|---|