PQsetnonblocking: * sets the PGconn's database connection non-blocking if the arg is true * or makes it blocking if the arg is false, this will not protect * you from PQexec(), you'll only be safe when using the non-blocking API. * Needs to be called only on a connected database connection. */
| 3746 | * Needs to be called only on a connected database connection. |
| 3747 | */ |
| 3748 | int |
| 3749 | PQsetnonblocking(PGconn *conn, int arg) |
| 3750 | { |
| 3751 | bool barg; |
| 3752 | |
| 3753 | if (!conn || conn->status == CONNECTION_BAD) |
| 3754 | return -1; |
| 3755 | |
| 3756 | barg = (arg ? true : false); |
| 3757 | |
| 3758 | /* early out if the socket is already in the state requested */ |
| 3759 | if (barg == conn->nonblocking) |
| 3760 | return 0; |
| 3761 | |
| 3762 | /* |
| 3763 | * to guarantee constancy for flushing/query/result-polling behavior we |
| 3764 | * need to flush the send queue at this point in order to guarantee proper |
| 3765 | * behavior. this is ok because either they are making a transition _from_ |
| 3766 | * or _to_ blocking mode, either way we can block them. |
| 3767 | * |
| 3768 | * Clear errorMessage in case pqFlush adds to it. |
| 3769 | */ |
| 3770 | resetPQExpBuffer(&conn->errorMessage); |
| 3771 | |
| 3772 | /* if we are going from blocking to non-blocking flush here */ |
| 3773 | if (pqFlush(conn)) |
| 3774 | return -1; |
| 3775 | |
| 3776 | conn->nonblocking = barg; |
| 3777 | |
| 3778 | return 0; |
| 3779 | } |
| 3780 | |
| 3781 | /* |
| 3782 | * return the blocking status of the database connection |