summaryrefslogtreecommitdiff
path: root/erts/emulator/test/driver_SUITE_data/peek_non_existing_queue_drv.c
blob: b69d75c31d5cfc2a32fdac287a5dccae740d3b53 (plain)
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
/* ``Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * The Initial Developer of the Original Code is Ericsson Utvecklings AB.
 * Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
 * AB. All Rights Reserved.''
 * 
 *     $Id$
 */

/*
 * Author: Rickard Green
 *
 * Description: Tests that port I/O queues can be flushed via:
 *		- ready_input(),
 *		- ready_output(),
 *		- timeout(),
 *		- driver_async() -> read_async(), and
 *		- event()
 */

#ifndef UNIX
#if !defined(__WIN32__)
#define UNIX 1
#endif
#endif

#if defined(DEBUG) || 0
#  define PRINTF(X) printf X
#else
#  define PRINTF(X)
#endif

#if defined(UNIX)
#include <stdio.h>
#include <string.h>
#elif defined(__WIN32__)
#include <windows.h>
#endif

#ifdef HAVE_UNISTD_H
#   include <unistd.h>
#endif

#include <errno.h>

#include "erl_driver.h"

#define PEEK_NONXQ_TEST 0
#define PEEK_NONXQ_WAIT 1

typedef struct {
    ErlDrvTermData caller;
    ErlDrvPort port;
    int cmd;
} PeekNonXQDrvData;

typedef struct {
    ErlDrvPort port;
    ErlDrvPDL pdl;    
} AsyncData;

static ErlDrvData start(ErlDrvPort, char *);
static void stop(ErlDrvData);
static ErlDrvSSizeT control(ErlDrvData, unsigned int,
			    char *, ErlDrvSizeT, char **, ErlDrvSizeT);
static void ready_async(ErlDrvData, ErlDrvThreadData);
static void async_test(void *);
static void async_wait(void *);
static void async_free(void *);
static void do_sleep(unsigned);

static ErlDrvEntry peek_non_existing_queue_drv_entry = { 
    NULL /* init */,
    start,
    stop,
    NULL /* output */,
    NULL /* ready_input */,
    NULL /* ready_output */,
    "peek_non_existing_queue_drv",
    NULL /* finish */,
    NULL /* handle */,
    control,
    NULL /* timeout */,
    NULL /* outputv */,
    ready_async,
    NULL /* flush */,
    NULL /* call */,
    NULL /* event */,
    ERL_DRV_EXTENDED_MARKER,
    ERL_DRV_EXTENDED_MAJOR_VERSION,
    ERL_DRV_EXTENDED_MINOR_VERSION,
    ERL_DRV_FLAG_USE_PORT_LOCKING,
    NULL /* handle2 */,
    NULL /* process_exit */
};

DRIVER_INIT(peek_non_existing_queue_drv)
{
    return &peek_non_existing_queue_drv_entry;
}

static ErlDrvData
start(ErlDrvPort port, char *command)
{
    PeekNonXQDrvData *dp = driver_alloc(sizeof(PeekNonXQDrvData));
    if (!dp) {
	errno = ENOMEM;
	return ERL_DRV_ERROR_ERRNO;
    }

    dp->port = port;
    return (ErlDrvData) dp;
}

static void stop(ErlDrvData drv_data)
{
    driver_free(drv_data);
}

static ErlDrvSSizeT control(ErlDrvData drv_data,
			    unsigned int command,
			    char *buf, ErlDrvSizeT len,
			    char **rbuf, ErlDrvSizeT rlen)
{
    PeekNonXQDrvData *dp = (PeekNonXQDrvData *) drv_data;
    unsigned int key = 0;
    char *res_str = "ok";
    ErlDrvSysInfo si;
    driver_system_info(&si, sizeof(ErlDrvSysInfo));
    if (si.async_threads == 0) {
	res_str = "skipped: No async-threads available";
	goto done;
    }

    dp->cmd = command;
    dp->caller = driver_caller(dp->port);

    switch (command) {
    case PEEK_NONXQ_TEST: {
	AsyncData *adp = driver_alloc(sizeof(AsyncData));
	if (!adp) {
	    res_str = "enomem";
	    goto done;
	}
	driver_enq(dp->port, "!", 1);
	adp->port = dp->port;
	adp->pdl = driver_pdl_create(dp->port);
	(void) driver_async(dp->port, &key, async_test, adp, async_free);
	break;
    }
    case PEEK_NONXQ_WAIT:
	(void) driver_async(dp->port, &key, async_wait, NULL, NULL);
	break;
    }

 done: {
	ErlDrvSSizeT res_len = strlen(res_str);
	if (res_len > rlen) {
	    char *abuf = driver_alloc(sizeof(char)*res_len);
	    if (!abuf)
		return 0;
	    *rbuf = abuf;
	}

	memcpy((void *) *rbuf, (void *) res_str, res_len);

	return res_len;
    }
}

static void ready_async(ErlDrvData drv_data, ErlDrvThreadData thread_data)
{
    PeekNonXQDrvData *dp = (PeekNonXQDrvData *) drv_data;
    if (dp->cmd == PEEK_NONXQ_WAIT) {
	ErlDrvTermData port_id = driver_mk_port(dp->port);
	ErlDrvTermData spec[] = {
	    ERL_DRV_PORT, port_id,
	    ERL_DRV_ATOM, driver_mk_atom("test_successful"),
	    ERL_DRV_TUPLE, 2
	};
	erl_drv_send_term(port_id,
			  dp->caller,
			  spec,
			  sizeof(spec) / sizeof(spec[0]));
    }
    if (thread_data)
	driver_free(thread_data);
}

static void async_test(void *vadp)
{
    SysIOVec *vec;
    int vlen = 4711;
    AsyncData *adp = (AsyncData *)vadp;

    do_sleep(1);

    driver_pdl_lock(adp->pdl);
    vec = driver_peekq(adp->port, &vlen);
    if (vlen >= 0 || vec)
	abort(); /* A crude way to fail the test, but what the ... */
    vlen = driver_sizeq(adp->port);
    if (vlen >= 0)
	abort(); /* ... */
    driver_pdl_unlock(adp->pdl);
}

static void async_wait(void *vadp)
{
    /* Will always be executed after async_test in the same thread */
}


static void async_free(void *vadp)
{
    driver_free(vadp);
}

static void
do_sleep(unsigned secs)
{
#ifdef __WIN32__
	Sleep((DWORD) secs*1000);
#else
	sleep(secs);
#endif
}