Skip to content

Commit 7a0562e

Browse files
committed
Move nogvl methods from results into separate file
1 parent 2ade154 commit 7a0562e

File tree

5 files changed

+99
-87
lines changed

5 files changed

+99
-87
lines changed

ext/tiny_tds/client.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <tiny_tds_ext.h>
22
#include <errno.h>
3+
#include <nogvl.h>
34

45
VALUE cTinyTdsClient;
56
extern VALUE mTinyTds, cTinyTdsError;

ext/tiny_tds/nogvl.c

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include <tiny_tds_ext.h>
2+
3+
void nogvl_setup(DBPROCESS *client) {
4+
GET_CLIENT_USERDATA(client);
5+
userdata->nonblocking = 1;
6+
userdata->nonblocking_errors_length = 0;
7+
userdata->nonblocking_errors = malloc(ERRORS_STACK_INIT_SIZE * sizeof(tinytds_errordata));
8+
userdata->nonblocking_errors_size = ERRORS_STACK_INIT_SIZE;
9+
}
10+
11+
void nogvl_cleanup(DBPROCESS *client) {
12+
GET_CLIENT_USERDATA(client);
13+
userdata->nonblocking = 0;
14+
userdata->timing_out = 0;
15+
/*
16+
Now that the blocking operation is done, we can finally throw any
17+
exceptions based on errors from SQL Server.
18+
*/
19+
short int i;
20+
for (i = 0; i < userdata->nonblocking_errors_length; i++) {
21+
tinytds_errordata error = userdata->nonblocking_errors[i];
22+
23+
// lookahead to drain any info messages ahead of raising error
24+
if (!error.is_message) {
25+
short int j;
26+
for (j = i; j < userdata->nonblocking_errors_length; j++) {
27+
tinytds_errordata msg_error = userdata->nonblocking_errors[j];
28+
if (msg_error.is_message) {
29+
rb_tinytds_raise_error(client, msg_error);
30+
}
31+
}
32+
}
33+
34+
rb_tinytds_raise_error(client, error);
35+
}
36+
37+
free(userdata->nonblocking_errors);
38+
userdata->nonblocking_errors_length = 0;
39+
userdata->nonblocking_errors_size = 0;
40+
}
41+
42+
void dbcancel_ubf(DBPROCESS *client) {
43+
GET_CLIENT_USERDATA(client);
44+
dbcancel(client);
45+
userdata->dbcancel_sent = 1;
46+
}
47+
48+
// No GVL Helpers
49+
RETCODE nogvl_dbsqlexec(DBPROCESS *client) {
50+
int retcode = FAIL;
51+
nogvl_setup(client);
52+
retcode = NOGVL_DBCALL(dbsqlexec, client);
53+
nogvl_cleanup(client);
54+
return retcode;
55+
}
56+
57+
RETCODE nogvl_dbsqlok(DBPROCESS *client) {
58+
int retcode = FAIL;
59+
GET_CLIENT_USERDATA(client);
60+
nogvl_setup(client);
61+
retcode = NOGVL_DBCALL(dbsqlok, client);
62+
nogvl_cleanup(client);
63+
userdata->dbsqlok_sent = 1;
64+
return retcode;
65+
}
66+
67+
RETCODE nogvl_dbresults(DBPROCESS *client) {
68+
int retcode = FAIL;
69+
nogvl_setup(client);
70+
retcode = NOGVL_DBCALL(dbresults, client);
71+
nogvl_cleanup(client);
72+
return retcode;
73+
}
74+
75+
RETCODE nogvl_dbnextrow(DBPROCESS * client) {
76+
int retcode = FAIL;
77+
nogvl_setup(client);
78+
retcode = NOGVL_DBCALL(dbnextrow, client);
79+
nogvl_cleanup(client);
80+
return retcode;
81+
}

ext/tiny_tds/nogvl.h

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef TINYTDS_NOGVL_H
2+
#define TINYTDS_NOGVL_H
3+
4+
#define NOGVL_DBCALL(_dbfunction, _client) ( \
5+
(RETCODE)(intptr_t)rb_thread_call_without_gvl( \
6+
(void *(*)(void *))_dbfunction, _client, \
7+
(rb_unblock_function_t*)dbcancel_ubf, _client ) \
8+
)
9+
10+
void dbcancel_ubf(DBPROCESS *client);
11+
RETCODE nogvl_dbnextrow(DBPROCESS * client);
12+
RETCODE nogvl_dbresults(DBPROCESS *client);
13+
RETCODE nogvl_dbsqlexec(DBPROCESS *client);
14+
RETCODE nogvl_dbsqlok(DBPROCESS *client);
15+
16+
#endif

ext/tiny_tds/result.c

-87
Original file line numberDiff line numberDiff line change
@@ -69,93 +69,6 @@ VALUE rb_tinytds_new_result_obj(tinytds_client_wrapper *cwrap) {
6969
return obj;
7070
}
7171

72-
// No GVL Helpers
73-
74-
#define NOGVL_DBCALL(_dbfunction, _client) ( \
75-
(RETCODE)(intptr_t)rb_thread_call_without_gvl( \
76-
(void *(*)(void *))_dbfunction, _client, \
77-
(rb_unblock_function_t*)dbcancel_ubf, _client ) \
78-
)
79-
80-
static void dbcancel_ubf(DBPROCESS *client) {
81-
GET_CLIENT_USERDATA(client);
82-
dbcancel(client);
83-
userdata->dbcancel_sent = 1;
84-
}
85-
86-
static void nogvl_setup(DBPROCESS *client) {
87-
GET_CLIENT_USERDATA(client);
88-
userdata->nonblocking = 1;
89-
userdata->nonblocking_errors_length = 0;
90-
userdata->nonblocking_errors = malloc(ERRORS_STACK_INIT_SIZE * sizeof(tinytds_errordata));
91-
userdata->nonblocking_errors_size = ERRORS_STACK_INIT_SIZE;
92-
}
93-
94-
static void nogvl_cleanup(DBPROCESS *client) {
95-
GET_CLIENT_USERDATA(client);
96-
userdata->nonblocking = 0;
97-
userdata->timing_out = 0;
98-
/*
99-
Now that the blocking operation is done, we can finally throw any
100-
exceptions based on errors from SQL Server.
101-
*/
102-
short int i;
103-
for (i = 0; i < userdata->nonblocking_errors_length; i++) {
104-
tinytds_errordata error = userdata->nonblocking_errors[i];
105-
106-
// lookahead to drain any info messages ahead of raising error
107-
if (!error.is_message) {
108-
short int j;
109-
for (j = i; j < userdata->nonblocking_errors_length; j++) {
110-
tinytds_errordata msg_error = userdata->nonblocking_errors[j];
111-
if (msg_error.is_message) {
112-
rb_tinytds_raise_error(client, msg_error);
113-
}
114-
}
115-
}
116-
117-
rb_tinytds_raise_error(client, error);
118-
}
119-
120-
free(userdata->nonblocking_errors);
121-
userdata->nonblocking_errors_length = 0;
122-
userdata->nonblocking_errors_size = 0;
123-
}
124-
125-
static RETCODE nogvl_dbsqlok(DBPROCESS *client) {
126-
int retcode = FAIL;
127-
GET_CLIENT_USERDATA(client);
128-
nogvl_setup(client);
129-
retcode = NOGVL_DBCALL(dbsqlok, client);
130-
nogvl_cleanup(client);
131-
userdata->dbsqlok_sent = 1;
132-
return retcode;
133-
}
134-
135-
static RETCODE nogvl_dbsqlexec(DBPROCESS *client) {
136-
int retcode = FAIL;
137-
nogvl_setup(client);
138-
retcode = NOGVL_DBCALL(dbsqlexec, client);
139-
nogvl_cleanup(client);
140-
return retcode;
141-
}
142-
143-
static RETCODE nogvl_dbresults(DBPROCESS *client) {
144-
int retcode = FAIL;
145-
nogvl_setup(client);
146-
retcode = NOGVL_DBCALL(dbresults, client);
147-
nogvl_cleanup(client);
148-
return retcode;
149-
}
150-
151-
static RETCODE nogvl_dbnextrow(DBPROCESS * client) {
152-
int retcode = FAIL;
153-
nogvl_setup(client);
154-
retcode = NOGVL_DBCALL(dbnextrow, client);
155-
nogvl_cleanup(client);
156-
return retcode;
157-
}
158-
15972
// Lib Backend (Helpers)
16073

16174
static RETCODE rb_tinytds_result_dbresults_retcode(VALUE self) {

ext/tiny_tds/tiny_tds_ext.h

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <sybfront.h>
1212
#include <sybdb.h>
1313

14+
#include <nogvl.h>
1415
#include <client.h>
1516
#include <result.h>
1617

0 commit comments

Comments
 (0)