1 /*
2 **  GNU Pth - The GNU Portable Threads
3 **  Copyright (c) 1999-2006 Ralf S. Engelschall <rse@engelschall.com>
4 **
5 **  This file is part of GNU Pth, a non-preemptive thread scheduling
6 **  library which can be found at http://www.gnu.org/software/pth/.
7 **
8 **  This library is free software; you can redistribute it and/or
9 **  modify it under the terms of the GNU Lesser General Public
10 **  License as published by the Free Software Foundation; either
11 **  version 2.1 of the License, or (at your option) any later version.
12 **
13 **  This library is distributed in the hope that it will be useful,
14 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 **  Lesser General Public License for more details.
17 **
18 **  You should have received a copy of the GNU Lesser General Public
19 **  License along with this library; if not, write to the Free Software
20 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 **  USA, or contact Ralf S. Engelschall <rse@engelschall.com>.
22 **
23 **  pth_ext.c: Pth extensions
24 */
25                              /* ``Killing for peace is
26                                   like fucking for virginity.''
27                                              -- Unknown  */
28 #include "pth_p.h"
29 
30 /*
31  * Sfio Extension:
32  *
33  * We provide an Sfio discipline which can be pushed on an Sfio_t* stream
34  * to use the Pth thread-aware I/O routines (pth_read/pth_write).
35  */
36 
37 #if PTH_EXT_SFIO
38 
pth_sfio_read(Sfio_t * f,Void_t * buf,size_t n,Sfdisc_t * disc)39 static ssize_t pth_sfio_read(Sfio_t *f, Void_t *buf, size_t n, Sfdisc_t *disc)
40 {
41     ssize_t rv;
42 
43     rv = pth_read(sffileno(f), buf, n);
44     return rv;
45 }
46 
pth_sfio_write(Sfio_t * f,const Void_t * buf,size_t n,Sfdisc_t * disc)47 static ssize_t pth_sfio_write(Sfio_t *f, const Void_t *buf, size_t n, Sfdisc_t *disc)
48 {
49     ssize_t rv;
50 
51     rv = pth_write(sffileno(f), buf, n);
52     return rv;
53 }
54 
pth_sfio_seek(Sfio_t * f,Sfoff_t addr,int type,Sfdisc_t * disc)55 static Sfoff_t pth_sfio_seek(Sfio_t *f, Sfoff_t addr, int type, Sfdisc_t *disc)
56 {
57     return sfsk(f, addr, type, disc);
58 }
59 
pth_sfio_except(Sfio_t * f,int type,Void_t * data,Sfdisc_t * disc)60 static int pth_sfio_except(Sfio_t *f, int type, Void_t* data, Sfdisc_t *disc)
61 {
62     int rv;
63 
64     switch (type) {
65         case SF_LOCKED:
66         case SF_READ:
67         case SF_WRITE:
68         case SF_SEEK:
69         case SF_NEW:
70         case SF_CLOSE:
71         case SF_FINAL:
72         case SF_DPUSH:
73         case SF_DPOP:
74         case SF_DBUFFER:
75         case SF_DPOLL:
76         case SF_READY:
77         case SF_SYNC:
78         case SF_PURGE:
79         default:
80             rv = 0; /* perform default action */
81     }
82     return rv;
83 }
84 
85 #endif /* PTH_EXT_SFIO */
86 
pth_sfiodisc(void)87 Sfdisc_t *pth_sfiodisc(void)
88 {
89 #if PTH_EXT_SFIO
90     Sfdisc_t *disc;
91 
92     if ((disc = (Sfdisc_t *)malloc(sizeof(Sfdisc_t))) == NULL)
93         return pth_error((SFdisc_t *)NULL, errno);
94     disc->readf   = pth_sfio_read;
95     disc->writef  = pth_sfio_write;
96     disc->seekf   = pth_sfio_seek;
97     disc->exceptf = pth_sfio_except;
98     return disc;
99 #else
100     return pth_error((Sfdisc_t *)NULL, ENOSYS);
101 #endif /* PTH_EXT_SFIO */
102 }
103 
104