File: aPOSIXaio.c

package info (click to toggle)
libmems 1.6.0%2B4725-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,148 kB
  • sloc: cpp: 21,579; ansic: 4,313; xml: 115; makefile: 107; sh: 26
file content (124 lines) | stat: -rw-r--r-- 3,603 bytes parent folder | download
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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "libMems/dmSML/aPOSIXaio.h"
#ifdef USE_POSIX_AIO

#include "libMems/dmSML/asyncio.h"
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

int OpenPAIO( aFILE * file, const char *path, int mode ){
	int flags = 0;
#ifdef O_LARGEFILE
	flags |= O_LARGEFILE;
#endif
	if(mode == A_READ){
		file->file_descriptor = open(path, flags | O_RDONLY, S_IREAD | S_IWRITE | S_IRGRP | S_IWGRP );
	}else{
		file->file_descriptor = open(path, flags | O_RDWR | O_CREAT | O_TRUNC,  S_IREAD | S_IWRITE | S_IRGRP | S_IWGRP);
	}
	if(file->file_descriptor < 0){
		
		perror(path);
	}
	return file->file_descriptor >= 0;
}

int ClosePAIO( aFILE * file ){
	return close( file->file_descriptor ) == 0;
}

int FillAIOStruct( aFILE * file, aIORec * rec ){
// fill the request data structure
	rec->aio_cb = (aiocb_t*) malloc( sizeof(aiocb_t));
	if(rec->aio_cb == 0)
		return 0;

	if( rec->pos != CURRENT_POS ){
		offset_t tmppos = rec->pos;
		tmppos >>= 32;
		file->filep_high = tmppos;
		// clear high bits.  Is this really necessary?
		tmppos = rec->pos;
		tmppos <<= 32;
		tmppos >>= 32;
		file->filep_low = tmppos;
	}

	rec->aio_cb->aio_fildes = file->file_descriptor;
	rec->aio_cb->aio_offset = file->filep_high;
	rec->aio_cb->aio_offset <<= 32;
	rec->aio_cb->aio_offset |= file->filep_low;
	rec->aio_cb->aio_buf = rec->buf;
	rec->aio_cb->aio_nbytes = rec->size * rec->count;
	rec->aio_cb->aio_reqprio = 0;
	memset(&(rec->aio_cb->aio_sigevent), 0, sizeof(struct sigevent) );
	return 1;
}

int WritePAIO( aFILE * file, aIORec * rec ){
        int req_error;
	if( FillAIOStruct( file, rec ) ){
		// request the io
		rec->aio_cb->aio_lio_opcode = LIO_WRITE;
		req_error = aio_write(rec->aio_cb);
		if(req_error == -1){
			perror("write");
//            printf( "aiocb->aio_filedes = %d\n", rec->aio_cb->aio_filedes );
//            printf( "aiocb->aio_offset = %llu\n", rec->aio_cb->aio_offset );
//            printf( "aiocb->aio_buf = %lx\n", rec->aio_cb->aio_buf );
//            printf( "aiocb->aio_nbytes = %llu\n", rec->aio_cb->aio_nbytes );
            printf( "aiocb->aio_reqprio = %d\n", rec->aio_cb->aio_reqprio );
		}
		return req_error == 0;
	}
	return 0;
}

int ReadPAIO( aFILE * file, aIORec * rec ){
	int req_error;
// fill the request data structure
	if( FillAIOStruct( file, rec ) ){
	// request the io
		rec->aio_cb->aio_lio_opcode = LIO_READ;
		req_error = aio_read(rec->aio_cb);
        if(req_error == -1){
                perror("write");
//                printf( "aiocb->aio_filedes = %d\n", rec->aio_cb->aio_filedes );
//                printf( "aiocb->aio_offset = %llu\n", rec->aio_cb->aio_offset );
//                printf( "aiocb->aio_buf = %lx\n", rec->aio_cb->aio_buf );
//                printf( "aiocb->aio_nbytes = %llu\n", rec->aio_cb->aio_nbytes );
                printf( "aiocb->aio_reqprio = %d\n", rec->aio_cb->aio_reqprio );
        }
		return req_error == 0;
	}
	return 0;
}

// PRECONDITION:  file->queuetail is not null
// simply queries wether the first request submitted to the file has
// completed yet.
int QueryLastCompletePAIO( const aFILE * file ){
	int rval;
	const struct aiocb * request_array[] = { (const struct aiocb*)file->queuetail->aio_cb };
	struct timespec zero_wait;

	zero_wait.tv_sec = 0;
	zero_wait.tv_nsec = 0;
	
	rval = aio_suspend(request_array, 1, &zero_wait);
	if(rval == 0){
		return 1; //why, shouldnt we tell the caller what finished?
	}else if(rval == -1)
		;
//		perror("aio_suspend");
	return 0;
}

#endif /* USE_POSIX_AIO */