Skip to content

Commit 26c8825

Browse files
committed
Move nogvl methods from results into separate file
1 parent cf84313 commit 26c8825

File tree

6 files changed

+113
-101
lines changed

6 files changed

+113
-101
lines changed

ext/tiny_tds/client.c

Lines changed: 2 additions & 1 deletion
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;
@@ -331,7 +332,7 @@ static VALUE rb_tinytds_execute(VALUE self, VALUE sql)
331332
VALUE result;
332333

333334
GET_CLIENT_WRAPPER(self);
334-
335+
335336
if (cwrap->closed || cwrap->userdata->closed) {
336337
rb_raise(cTinyTdsError, "closed connection");
337338
return Qnil;

ext/tiny_tds/nogvl.c

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

ext/tiny_tds/nogvl.h

Lines changed: 16 additions & 0 deletions
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

Lines changed: 0 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -73,103 +73,6 @@ VALUE rb_tinytds_new_result_obj(tinytds_client_wrapper *cwrap)
7373
return obj;
7474
}
7575

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

17578
static RETCODE rb_tinytds_result_dbresults_retcode(VALUE self)

ext/tiny_tds/tiny_tds_ext.h

Lines changed: 1 addition & 0 deletions
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

test/client_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ class ClientTest < TinyTds::TestCase
273273
assert !client.canceled?
274274

275275
result = client.execute("SELECT 1 as [one]")
276-
assert_equal [{"one"=>1}], result.to_a
276+
assert_equal [{"one" => 1}], result.to_a
277277
assert_client_works(client)
278278
end
279279

@@ -285,7 +285,7 @@ class ClientTest < TinyTds::TestCase
285285
assert !client.canceled?
286286

287287
result = client.execute("SELECT 1 as [one]")
288-
assert_equal [{"one"=>1}], result.to_a
288+
assert_equal [{"one" => 1}], result.to_a
289289
assert_client_works(client)
290290
end
291291

@@ -300,7 +300,7 @@ class ClientTest < TinyTds::TestCase
300300
assert !client.canceled?
301301

302302
result = client.execute("SELECT 1 as [one]")
303-
assert_equal [{"one"=>1}], result.to_a
303+
assert_equal [{"one" => 1}], result.to_a
304304
assert_client_works(client)
305305
end
306306
end

0 commit comments

Comments
 (0)